Android - MPAndroid Chart showing x axis value - java

I have a listview with data in one activity and show that data in a graph in another activity. ListView Data. and displayed on graph.
I want to show the x axis associated with the value("Running", "Swimming")
I'm using an intent to send the value to the graph page and here is the code i'm using to populate the graph.
ArrayList<String> cardioValue = new ArrayList<String>();
ArrayList<String> cardioCategory = new ArrayList<String>();
cardioValue = intent.getStringArrayListExtra("cardioValue");
cardioCategory = intent.getStringArrayListExtra("cardioCategory");
ArrayList<BarEntry> barEntries = new ArrayList<>();
for(int i=0; i<cardioValue.size(); i++ ){
barEntries.add(new BarEntry( i+1, Integer.parseInt(cardioValue.get(i))));
}

You will have to use a value formatter to achieve this.
String[] cardioValue = {"", "Running, "Swimming"};
XAxis xaxis = mChart.getXAxis();
xaxis.setValueFormatter(new IndexAxisValueFormatter(cardioValue));
I have kept the first value of the string array empty because your first bar is being formed at value 1.
You will have to do the same with array lists.
Link to documentaion.
Working example.

Related

Applying css to LineChart JavaFx chart

I am filling a LineChat chart with data and sometimes I need to redraw it. The drawGraph() method is attached to the button click event (we press it to redraw the graph). I want the first data series to have transparent markers and the second data series to have transparent lines. For this I use css. On the first call to the drawGraph() method, everything works fine, and I get this picture:
It should be
I try to make it so that every time I redraw the chart, I get the 1st picture
But from the next redrawings, the following picture starts to be displayed every other time:
It should not be
That is, a graph is displayed every other time without applying css to it, and one data series overlaps another
Style sheet:
/* Removes points from a data series */
.default-color0.chart-line-symbol {
-fx-background-color: transparent;
}
/* Removes lines from a data series */
.default-color1.chart-series-line {
-fx-stroke: transparent;
}
Chart redrawing method (from ApplicationController):
double[] X = new double[] {1,2,3,4,5};
double[] Y = new double[] {1.9,5.5,10,15,21};
public void drawGraph() {
//clear the Chart
lineChart.getData().clear();
//Data series #1
ObservableList<XYChart.Data> datas = FXCollections.observableArrayList();
XYChart.Series series = new XYChart.Series();
//Filling series #1
for(int i=0; i< X.length; i++){
datas.add(new XYChart.Data(X[i],Y[i]));
}
series.setData(datas);
//Data series #2
ObservableList<XYChart.Data> datas1 = FXCollections.observableArrayList();
XYChart.Series series1 = new XYChart.Series();
//Filling series #2
for(int i=0; i< X.length; i++){
datas1.add(new XYChart.Data(X[i],Y[i]));
}
series1.setData(datas1);
//lineChart.setStyle();
lineChart.getStylesheets().add(HelloApplication.class.getResource("style.css")
.toExternalForm());
lineChart.getData().add(series);
lineChart.getData().add(series1);
}
It was bad practice to create new data series and place them on the chart instead of the old ones. Also, it was not worth using a style sheet for each new data redrawing.
What I did was to fill in the graph for the first time, and when it was redrawn, I replaced it in the series as follows.
//We get a series:
XYChart.Series<Number, Number> series = lineChart.getData().get(0);
//Let's remove all elements in the series:
series.getData().clear();
//We add elements to the series like this:
series.getData().add(new LineChart.Data<Number,Number>(1, 2));

How to draw grouped bar chart in Android with different number of columns in each group?

This is the chart that I want to create (click on the link to view image):
Desired chart
I have researched a lot about Grouped Bar Chart in MPAndroidChart but that library seems to restrict. Each group must have the same number of columns and the column order must be consistent. Does anyone know how to create a grouped bar chart like in my image using MPAndroidChart (or any other libraries)?
Please help me. Thank you in advance.
Could you try this code?
List<String> xValues = ...; // "Shop1", "Shop2", "Shop3"
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyValueFormatter(xValues));
// create 2 datasets
BarDataSet set1 = new BarDataSet(valuesProduct1, "Product1"); //valuesProduct1 are the values corresponding to the shops,
set1.setColor(Color.BLUE);
BarDataSet set2 = new BarDataSet(valuesProduct2, "Product2");
set2.setColor(Color.RED);
BarData data = new BarData(set1, set2);
chart.setData(data);
chart.groupBars(...); // available since release v3.0.0
chart.invalidate(); // refresh
If you have some troubles where the shop has no products, just put zero on the concrete position

MPChart xAxis values not aligned

I am trying to implement grouped MPAndroid Bar chart. I have a group of 2 datasets that i want to display. The problem is that the xaxis values are not center aligned with the bar chart (as per the screenshot). I checked other questions as well and implemented the following answers provided.
I want to make the labels center aligned with the grouped bars.
float barSpace = 0.02f;
float groupSpace = 0.3f;
int groupCount = 2;
data.setBarWidth(0.155f);
pvaAmount_chart.getXAxis().setAxisMinimum(0);
pvaAmount_chart.getXAxis().setAxisMaximum(0 + pvaAmount_chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
pvaAmount_chart.groupBars(0, groupSpace, barSpace);
pvaAmount_chart.getXAxis().setCenterAxisLabels(true);
pvaAmount_chart.notifyDataSetChanged();
When entering groupcount=2 as 2 types of bars:
When entering groupcount=4 number of grouped charts:
i'm doing it like this :
String [] values = new String[insert the lenght of your array]
for (int blabla=0;i<values[lenght];blabla++){
String dayOfTheWeek = dateFormat.format(mydate);
vales[blabla] = dayOfTheWeek //in my case
}
xAxis.setGranularity(1.0f); //
xAxis.setCenterAxisLabels(false);
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
List<String> stringList = new ArrayList<String>(Arrays.asList(values));
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues((ArrayList<String>) stringList)));
i'm using : 'com.github.PhilJay:MPAndroidChart:v3.1.0'

Achartengine display values inside the series areas and horizontal legend

I have tried the following with the default renderer
CategorySeries series = new CategorySeries("First test");
int numSlide = portions.length;
for (int i = 0; i < numSlide; i++){
series.add(seriesNames[i]+" ("+portions[i]+" %)", portions[i]);
}
DefaultRenderer defaultRenderer = new DefaultRenderer();
SimpleSeriesRenderer simpleSeriesRenderer = null;
for (int i = 0; i < numSlide; i++){
simpleSeriesRenderer = new SimpleSeriesRenderer();
simpleSeriesRenderer.setColor(colors[i]);
simpleSeriesRenderer.setChartValuesFormat(new DecimalFormat("###,###,##0.0"));
defaultRenderer.addSeriesRenderer(simpleSeriesRenderer);
}
defaultRenderer.setInScroll(true);
defaultRenderer.setZoomButtonsVisible(false);
defaultRenderer.setZoomEnabled(true);
defaultRenderer.setLabelsTextSize(18); //value size
defaultRenderer.setLabelsColor(R.color.primary_dark);
defaultRenderer.setShowLegend(false);
defaultRenderer.setClickEnabled(true);
defaultRenderer.setPanEnabled(true);
defaultRenderer.setShowLabels(true);
defaultRenderer.setShowLegend(true);
//return the pie chart view
return ChartFactory.getPieChartView(context, series, defaultRenderer);
Now the above produces
What am looking forward to get is to have the percentage values eg: 2% inside the chart area something which looks like this
How do i get the percentage values to be displayed inside the charts?
You want to display values and hide labels. Do that like so in your renderer:
defaultRenderer.setShowLabels(false);
defaultRenderer.setDisplayValues(true);
Also, your series names should probably not contain the values, so change the following line
series.add(seriesNames[i]+" ("+portions[i]+" %)", portions[i]);
to
series.add(seriesNames[i], portions[i]);

Use images as data points in line graph - Android

I am new to Android Development and I have to make line graph.
The graph I am trying to make is very customized i.e. using images as the data point.
There are lots of open source libraries but I cant use images as data points and they should be clickable.
Chart I am trying to make
i'm to late in answering this question but i have done recently this type of work using MPChartAndroid
this is the simple code to set icons you can check complete example at this link
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) - 30;
values.add(new Entry(i, val, getResources().getDrawable(R.drawable.star)));
}
LineDataSet set1;
set1 = LineDataSet(values, "")
set1.setDrawIcons(true)
chart.setData(data);

Categories

Resources