Chart frame hellocharts (2) Bar chart

Source: Internet
Author: User

1.

2.xml Code

Activity_column_chart.xml

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/container"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
/>
Fragment_column_chart.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
>
<lecho.lib.hellocharts.view.columnchartview
Android:id= "@+id/chart"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</lecho.lib.hellocharts.view.ColumnChartView>
</RelativeLayout>

3.java Code

Columnchartactivity.java
public class Columnchartactivity extends Appcompatactivity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_column_chart);
if (savedinstancestate = = null) {
Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container, New Placeholderfragment ()). commit ();
}
}

/**
* A fragment containing a column chart.
*/
public static class Placeholderfragment extends Fragment {

private static final int default_data = 0;
private static final int subcolumns_data = 1;
private static final int stacked_data = 2;
private static final int negative_subcolumns_data = 3;
private static final int negative_stacked_data = 4;

Private Columnchartview chart;
Private columnchartdata data;
Private Boolean hasaxes = true;
Private Boolean hasaxesnames = true;
Private Boolean haslabels = false;
Private Boolean haslabelforselected = false;
private int dataType = Default_data;

Public Placeholderfragment () {
}

@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
Sethasoptionsmenu (TRUE);
View Rootview = inflater.inflate (R.layout.fragment_column_chart, container, false);

Chart = (Columnchartview) Rootview.findviewbyid (R.id.chart);
Chart.setonvaluetouchlistener (New Valuetouchlistener ());

Generatedata ();

return rootview;
}

private void Generatedefaultdata () {
int numsubcolumns = 1;
int numcolumns = 8;
Column can has many subcolumns, here by default I use 1 subcolumn in each of 8 columns.
list<column> columns = new arraylist<column> ();
List<subcolumnvalue> values;
for (int i = 0; i < NumColumns; ++i) {

values = new arraylist<subcolumnvalue> ();
for (int j = 0; j < Numsubcolumns; ++j) {
Values.add (new Subcolumnvalue (float) math.random () * 50f + 5, Chartutils.pickcolor ()));
}

Column column = new column (values);
Column.sethaslabels (Haslabels);
Column.sethaslabelsonlyforselected (haslabelforselected);
Columns.Add (column);
}

data = new Columnchartdata (columns);

if (hasaxes) {
Axis AxisX = new Axis ();
Axis Axisy = new Axis (). Sethaslines (True);
if (hasaxesnames) {
Axisx.setname ("Axis X");
Axisy.setname ("Axis Y");
}
Data.setaxisxbottom (AxisX);
Data.setaxisyleft (Axisy);
} else {
Data.setaxisxbottom (NULL);
Data.setaxisyleft (NULL);
}

Chart.setcolumnchartdata (data);

}

/**
* Generates columns with Subcolumns, columns has larger separation than subcolumns.
*/
private void Generatesubcolumnsdata () {
int numsubcolumns = 4;
int numcolumns = 4;
Column can has many subcolumns, here I use 4 subcolumn in each of 8 columns.
list<column> columns = new arraylist<column> ();
List<subcolumnvalue> values;
for (int i = 0; i < NumColumns; ++i) {

values = new arraylist<subcolumnvalue> ();
for (int j = 0; j < Numsubcolumns; ++j) {
Values.add (new Subcolumnvalue (float) math.random () * 50f + 5, Chartutils.pickcolor ()));
}

Column column = new column (values);
Column.sethaslabels (Haslabels);
Column.sethaslabelsonlyforselected (haslabelforselected);
Columns.Add (column);
}

data = new Columnchartdata (columns);

if (hasaxes) {
Axis AxisX = new Axis ();
Axis Axisy = new Axis (). Sethaslines (True);
if (hasaxesnames) {
Axisx.setname ("Axis X");
Axisy.setname ("Axis Y");
}
Data.setaxisxbottom (AxisX);
Data.setaxisyleft (Axisy);
} else {
Data.setaxisxbottom (NULL);
Data.setaxisyleft (NULL);
}

Chart.setcolumnchartdata (data);

}

/**
* Generates columns with stacked subcolumns.
*/
private void Generatestackeddata () {
int numsubcolumns = 4;
int numcolumns = 8;
Column can has many stacked subcolumns, here I use 4 Stacke subcolumn in each of 4 columns.
list<column> columns = new arraylist<column> ();
List<subcolumnvalue> values;
for (int i = 0; i < NumColumns; ++i) {

values = new arraylist<subcolumnvalue> ();
for (int j = 0; j < Numsubcolumns; ++j) {
Values.add (new Subcolumnvalue (float) math.random () * 20f + 5, Chartutils.pickcolor ()));
}

Column column = new column (values);
Column.sethaslabels (Haslabels);
Column.sethaslabelsonlyforselected (haslabelforselected);
Columns.Add (column);
}

data = new Columnchartdata (columns);

Set stacked flag.
Data.setstacked (TRUE);

if (hasaxes) {
Axis AxisX = new Axis ();
Axis Axisy = new Axis (). Sethaslines (True);
if (hasaxesnames) {
Axisx.setname ("Axis X");
Axisy.setname ("Axis Y");
}
Data.setaxisxbottom (AxisX);
Data.setaxisyleft (Axisy);
} else {
Data.setaxisxbottom (NULL);

Data.setaxisyleft (NULL);
}

Chart.setcolumnchartdata (data);
}

private void Generatenegativesubcolumnsdata () {

int numsubcolumns = 4;
int numcolumns = 4;
list<column> columns = new arraylist<column> ();
List<subcolumnvalue> values;
for (int i = 0; i < NumColumns; ++i) {

values = new arraylist<subcolumnvalue> ();
for (int j = 0; j < Numsubcolumns; ++j) {
int sign = Getsign ();
Values.add (new Subcolumnvalue (float) math.random () * 50f * sign + 5 * sign, Chartutils.pickcolor
()));
}

Column column = new column (values);
Column.sethaslabels (Haslabels);
Column.sethaslabelsonlyforselected (haslabelforselected);
Columns.Add (column);
}

data = new Columnchartdata (columns);

if (hasaxes) {
Axis AxisX = new Axis ();
Axis Axisy = new Axis (). Sethaslines (True);
if (hasaxesnames) {
Axisx.setname ("Axis X");
Axisy.setname ("Axis Y");
}
Data.setaxisxbottom (AxisX);
Data.setaxisyleft (Axisy);
} else {
Data.setaxisxbottom (NULL);
Data.setaxisyleft (NULL);
}

Chart.setcolumnchartdata (data);
}

private void Generatenegativestackeddata () {

int numsubcolumns = 4;
int numcolumns = 8;
Column can has many stacked subcolumns, here I use 4 Stacke subcolumn in each of 4 columns.
list<column> columns = new arraylist<column> ();
List<subcolumnvalue> values;
for (int i = 0; i < NumColumns; ++i) {

values = new arraylist<subcolumnvalue> ();
for (int j = 0; j < Numsubcolumns; ++j) {
int sign = Getsign ();
Values.add (new Subcolumnvalue (float) math.random () * 20f * sign + 5 * sign, chartutils.pickcolor ());
}

Column column = new column (values);
Column.sethaslabels (Haslabels);
Column.sethaslabelsonlyforselected (haslabelforselected);
Columns.Add (column);
}

data = new Columnchartdata (columns);

Set stacked flag.
Data.setstacked (TRUE);

if (hasaxes) {
Axis AxisX = new Axis ();
Axis Axisy = new Axis (). Sethaslines (True);
if (hasaxesnames) {
Axisx.setname ("Axis X");
Axisy.setname ("Axis Y");
}
Data.setaxisxbottom (AxisX);
Data.setaxisyleft (Axisy);
} else {
Data.setaxisxbottom (NULL);
Data.setaxisyleft (NULL);
}

Chart.setcolumnchartdata (data);
}

private int getsign () {
int[] Sign = new int[]{-1, 1};
return Sign[math.round (float) math.random ());
}

private void Generatedata () {
Switch (dataType) {
Case Default_data:
Generatedefaultdata ();
Break
Case Subcolumns_data:
Generatesubcolumnsdata ();
Break
Case Stacked_data:
Generatestackeddata ();
Break
Case Negative_subcolumns_data:
Generatenegativesubcolumnsdata ();
Break
Case Negative_stacked_data:
Generatenegativestackeddata ();
Break
Default
Generatedefaultdata ();
Break
}
}

Private class Valuetouchlistener implements Columnchartonvalueselectlistener {

@Override
public void onvalueselected (int columnindex, int subcolumnindex, subcolumnvalue value) {
Toast.maketext (Getactivity (), "Selected:" + value, Toast.length_short). Show ();
}

@Override
public void onvaluedeselected () {
TODO auto-generated Method Stub

}

}

}

}

Chart frame hellocharts (2) Bar chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.