Java MP androidchart bar chart – how to group bars at random X-axis intervals between groups?
I want to make a bar chart in which three different data sets are combined at each data point, as shown below:
However, I can't use the groupbars method provided by the library to group these bars together, because no matter what x value I set for the items, it will group them according to the interval I specify in its parameters
For example, if I generate a dataset with an entry x value of {0,5,13,17... 50} and call 'groupbars', all my entries will be collected with an x value, as shown below:
What I want is a bar chart that groups each group. Each bar chart is visible at its specified x value. If I only delete the groupbars call, I will get information similar to what I want, but because the bar charts overlap, they will not be exactly the same, as shown below:
How to obtain results similar to the above images, but the bars of each dataset are completely visible? This is the code I use to generate datasets and group bars:
ArrayList<BarEntry> happinessValues = new ArrayList<>();
ArrayList<BarEntry> stressValues = new ArrayList<>();
ArrayList<BarEntry> painValues = new ArrayList<>();
for (int i = 0; i < 50; ++i) {
happinessValues.add(new BarEntry(
i,
datapoint.getHappiness()));
stressValues.add(new BarEntry(
i,
datapoint.getStress()));
painValues.add(new BarEntry(
i,
datapoint.getPain()));
}
HappinessDataset happyDataset;
BarDataSet stressDataset, painDataset;
happyDataset = new HappinessDataset(happinessValues, "Happiness");
stressDataset = new BarDataSet(stressValues, "Stress");
painDataset = new BarDataSet(painValues, "Pain");
BarData data = new BarData(happyDataset, stressDataset, painDataset);
mChart.setData(data);
mChart.getXAxis().setAxisMinimum(0);
mChart.getXAxis().setAxisMaximum(50);
float groupSpace = 0.4f;
float barSpace = 0f; // x3 DataSet
float barWidth = 0.2f; // x3 DataSet
// (0.2 + 0) * 3 + 0.4 = 1.00 -> interval per "group"
mChart.groupBars(startTime, groupSpace, barSpace);
resolvent:
I have solved this problem by modifying the x value and width of each bar entry
I create a new bardata class with three datasets and set the bar width (we call it bar_width) to 0.2 (that is, the three bar spaces account for 0.6 units in total and the spacing after the dataset is 0.4 units)
For any given bar input, I place the first bar at the desired x value (I for short) and the second bar at the x value I bar_ At width, place the third bar at I 2 * bar_ At width, the result is a set of 3 bar entries centered on any x value I want, as follows:
Therefore, in my above code, modify the bar entry creation code as follows:
final float BAR_WIDTH = 0.2f;
happinessValues.add(new BarEntry(
i,
datapoint.getHappiness()));
stressValues.add(new BarEntry(
i + BAR_WIDTH,
datapoint.getStress()));
painValues.add(new BarEntry(
i + 2 * BAR_WIDTH,
datapoint.getPain()));
mChart.getBarData().setBarWidth(BAR_WIDTH);