I need to plot a stacked line chart using JFreeChart, but there is no predefined method for creating stacked line chart. I thought stacked area chart without color filled inside and with area outlined makes a stacked line chart, but still not know how to execute that. Also, I tried adding the values in the dataset and plot using LineAndShapeRenderer; that's worked but altering the dataset seems to be a costly operation.
Note : My dataset is a category dataset and JFreeChart version 1.0.14. Some code example can be more helpful.
This is the default dataset :
private static DefaultCategoryDataset createCategoryDataset(JSONArray columns, JSONArray rows, JSONArray data) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < columns.length(); i++) {
for (int j = 0; j < data.length(); j++) {
JSONArray singleRow = data.getJSONArray(j);
dataset.addValue(singleRow.getInt(i) , rows.getString(j), columns.getString(i));
}
}
return dataset;
}
This is the dataset changed for stacked line chart:
private static DefaultCategoryDataset createCategoryDataset(JSONArray columns, JSONArray rows, JSONArray data) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < columns.length(); i++) {
int sum = 0;
for (int j = 0; j < data.length(); j++) {
JSONArray singleRow = data.getJSONArray(j);
dataset.addValue(sum += singleRow.getInt(i) , rows.getString(j), columns.getString(i));
}
}
return dataset;
}
Example parameters:
columns = [A,B,C]
rows = [X,Y,Z]
data = [ [0,0,1],[0,1,0],[1,0,0]]