Graph not rendering

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
marshallarts
Posts: 1
Joined: Tue Aug 06, 2019 2:54 am
antibot: No, of course not.

Graph not rendering

Post by marshallarts » Wed Aug 07, 2019 2:38 am

Hello. Apologies for this question, it's probably a very basic thing. I've been developing software for decades but am just getting into Java, and just discovered JFreeChart. In my app I want to show a chart on a TabItem within a TabFolder. It needs to show 2 line series in different colours, and the data is obtained by querying another computer which returns a packet of XML. I've been trawling forums and samples etc, and have got to the point where I do see a graph, but it has no lines on it. I'm clearly doing something wrong, or in the wrong order, but JFreechart is so big and complex that I'm struggling to track it down.

Following is my code which populates the 2 XYSeries objects then tries to set up the graph and place it on the TabItem. The XML data (which is from 2 solar inverters) will contain 2 <Inverter> nodes, each of which contains a number of <Reading> nodes. Each of those contains child nodes for <Timestamp> and <Pac>, which are used to derive the X and Y values of each point. The variable nList contains the node list of <Inverter> nodes. Debugging shows that the 2 XYSeries objects are getting populated correctly, I believe. So I'm sure it's after that where my problem is.

// Set up the data series and graph
XYSeriesCollection data = new XYSeriesCollection();
XYSeries seriesC = new XYSeries("Conergy");
XYSeries seriesS = new XYSeries("SolarEdge");

for (i=0; i<nList.getLength(); i++) { // inverter loop
el=(Element)nList.item(i);
sInvName = el.getAttribute("Name");
rList=el.getElementsByTagName("Reading");
//JOptionPane.showMessageDialog(null, rList.getLength() + " readings for inverter " + el.getAttribute("Name"));
for (j=0; j<rList.getLength(); j++) { // readings loop
el2=(Element)rList.item(j); // get the Reading node
dt=sdf.parse(el2.getElementsByTagName("TimeStamp").item(0).getTextContent());
X=(dt.getHours() * 60) + dt.getMinutes(); // compute X value
Y = Float.parseFloat(el2.getElementsByTagName("Pac").item(0).getTextContent());
if (sInvName.contentEquals("CONERGY")) {
seriesC.add(X,Y);
} else {
seriesS.add(X,Y);
}

}
}

// Create graph and plot
data.addSeries(seriesC);
data.addSeries(seriesS);
XYLineAndShapeRenderer xyrC = new XYLineAndShapeRenderer();
xyrC.setSeriesPaint(0,Color.BLUE);
xyrC.setLinesVisible(true);
XYLineAndShapeRenderer xyrS = new XYLineAndShapeRenderer();
xyrS.setSeriesPaint(0,Color.RED);
xyrS.setLinesVisible(true);

JFreeChart graph = ChartFactory.createXYLineChart(new SimpleDateFormat("d-MMMM-yyyy").format(new Date()), "Time", "Power W", data, PlotOrientation.VERTICAL,true,false,false);
XYPlot xyp=graph.getXYPlot();
xyp.setBackgroundPaint(Color.WHITE);
xyp.setDataset(data);
xyp.setRenderer(0, xyrC);
xyp.setRenderer(1, xyrS);
ChartPanel panel = new ChartPanel(graph);

// Place graph on TabItem
Composite cmpToday = new Composite(tabFolder, SWT.EMBEDDED);
cmpToday.setLayout(new GridLayout());
Frame frm = SWT_AWT.new_Frame(cmpToday);
frm.add(panel);
panel.setPreferredSize(new java.awt.Dimension(500,300));

Post Reply