Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid null in dataset for line chart

I'm trying to plot a LineChart using DefaultCategoryDataset and the problem I'm facing is, when I try to plot a null value in between two values either the connection between the two points is lost (i.e the line connecting the two points doesnot appear), or else I have to skip the null value(by performing a null check on every point in DataSet) in order to connect the other two points, which results in the x-axis point not getting plotted on my graph.

For example: if I have Unit1, Unit2, Unit3 on x-axis and some values, say 10, 20, 30 for each, it works fine and plots an inclined line. But instead of 20 in Unit2, if I set null, i get only two points ploted on my graph without a line connecting those two points.

In order to get the line plotted, I have to skip the Unit2 point from my x-axis. But what I want to do is i want to connect Unit1 to Unit3 without skipping Unit2(and its value remains null), I don't want to plot 0 instead of null.

Is there any way to do this ?

like image 526
arshithp Avatar asked Nov 13 '22 03:11

arshithp


1 Answers

You could use two almost identical datasets but two renderers.

The first dataset should contain all data including null-values. Configure a renderer for this dataset to only draw shapes (but no lines). You'll end up with a shape for every datapoint that is not `null.

The second dataset should contain only data that is not null. Configure another renderer for this dataset to only draw lines (but no shapes). You'll get a line that just skips the null-datapoint but is continous.

Code for this could be like this:

// create your chart here...

// configure first renderer
LineAndShapeRenderer renderer = LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseLinesVisible(false);

// configure second renderer
LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(true, false);
plot.setDataset(1, dataset2);
plot.setRenderer(1, renderer2);

hth,
- martin

like image 71
Martin Höller Avatar answered Dec 04 '22 14:12

Martin Höller