I am searching for the commands to change the String for X and Y Axis at runtime. I have language menu on my gui. As soon as the button for a specific language is pressed I invoke following methods:
public void resetLanguage(Locale locale)
{
displayLanguage = locale;
resources = ResourceBundle.getBundle("DTTUI-Text", displayLanguage);
chart.setTitle(resources.getString("GUI_CHART_TITLE"));
measurement.setDescription(resources.getString("GUI_CHART_GRAPH_NAME"));
}
chart is a JFreeChart and measurement is a XYSeries. Anyone knows how to find the setter methods for the axis labels? Thanks in advance!
I created my chart like this:
XYSeriesCollection collection = new XYSeriesCollection();
XYSeries series = new XYSeries("Title");
series.add(0, 0);
series.add(10,10);
collection.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart("Title", "x",
"y", collection);
Related
I'm trying to to make a chart that takes data from serial port and plot them in y axes and i want current time in x axes.. I think that i set my code correctly because i managed to run it as XY chart now in TimeSeries chart my only issue is that in method series.add(TIME, SERIALDATA); i dont know how to initialize TIME , i know that i want an object RegularTimePeriod but i dont know how to do that..
here is the code.. i know that only some lines are missing please help me to find them...
void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 817, 525);
final TimeSeries series = new TimeSeries("Charts");
final SerialDataReceived serialdataprint = new SerialDataReceived();
final TimeSeriesCollection data = new TimeSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart(
"Tmperature IN",
"Time",
"C",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setBounds(10, 11, 477, 224);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
chartPanel.setVisible(true);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(chartPanel);
chartPanel.setLayout(null);
Thread outtempthread=new Thread() { //THREAD THAT RUNS ALL THE TIME
public void run() {
try {
while (true){
Thread.sleep(2000);
double intemp = serialdataprint.getintemp(); //THIS WHERE I TAKE MY SERIAL DATA
series.add(I WANT TO DISPLAY HERE LETS SAY 13:23:15, intemp); //HERE IS MY PROBLEM
}}
catch (InterruptedException ie) {}
}
};
outtempthread.start();
}
I've only ever used TimeSeries measured in days so I used the org.jfree.data.time.Day class.
Here's the jfreechart javadoc for all the different time classes : http://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/package-summary.html
Try out a few and see what's right for you.
Since you appear to only need hour,minute second of a single day, you might be able to use the Second class.
Here is how you would make a TimeSeries that way:
int todaysDay =...
int todaysMonth =...
int todaysYear =...
TimeSeries series = new TimeSeries(name, Second.class);
//this should mark 'inTemp' as 13:23:15
series.add(new Second(15,23,13,todaysDay, todaysMonth, todaysYear),
inTemp);
ok!! finally i found the solution! i don't know if is the correct way but it works an now i have real time in my chart every time my serial port updates here is the fix code!
String timeStamp1 = new SimpleDateFormat("mm").format(Calendar.getInstance().getTime());
int minute = Integer.parseInt(timeStamp1);
double intemp = serialdataprint.getintemp();
series.addOrUpdate(new Minute(minute,hour), intemp);
A couple of pointers:
The ChartFactory.createXYLineChart() method will create a line chart where both the X and Y axes are numerical. Try the createTimeSeriesChart() to get a chart that shows dates on the X axis (or create a new DateAxis() instance and call plot.setDomainAxis() to change the X axis);
The TimeSeriesCollection class is a good dataset to use for time series data if you need the structure that it provides (it enforces a regular time period and prevents duplicates among other things). However, bear in mind that it is simply an implementation of the XYDataset interface where the x-values returned are "milliseconds since 1-Jan-1970" (the standard encoding of "dates" in Java). You can simplify your code by using an XYSeriesCollection (which also implements the XYDataset interface), and call System.currentTimeInMillis() to get the current x-value when new data comes in. The date axis on your chart will take care of presenting a date scale for this data.
i have two sets of data
int[] x1 = {1,2,3,4,5,6,7,8,9,10};
int[] y1 = {1,2,3,5,6,8,9,10,14,11};
int[] x2 = {1,2,3,4,5,6,7,8,9,10};
int[] y2 = {0,2,3,5,0,8,9,8,14,11};
int[] z2 = {1,2,3,1,2,3,1,2,3,1};
I want to plot the x1,y1 as an XYLineChart and then plot x2,y2 as a scatter on the same plot without a line.
I also need each scatter point of xy,y2 to be a different color depending on the value of z2 (1=Color.red, 2=Color.green, 3=Color.blue)
How can i do this?
So far i have:
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout(0, 0));
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("series1");
for(int i=0; i<x1.length; i++){
series1.add(x1[i],y1[i]);
}
dataset.add(series1);
JFreeChart chart = ChartFactory.createXYLineChart("Title", "x", "y", dataset, PlotOrientation.VERTICAL, false, false, false);
ChartPanel cp = new ChartPanel(chart);
panel_1.add(cp, BorderLayout.CENTER);
This gets the line graph sorted. I now need to code the scatter plot for x2,y2 (with colors described above) which is where im stuck.
The createXYLineChart() method will create a chart that uses an XYLineAndShapeRenderer. So fetch the renderer from the plot and cast it to XYLineAndShapeRenderer. Then you can call the methods setSeriesLinesVisible() and setSeriesShapesVisible() to control, for each series, whether shapes and/or lines are drawn for the data items. That way you can use a single renderer and dataset, which makes things simpler.
Your requirement to change the colors depending on another data value requires a little more work. You should subclass the XYLineAndShapeRenderer class and override the getItemPaint(int, int) method. Here you can return any color you want for a data item. The default implementation looks at the series index and returns the color for the series. You need to look at the item index as well, then do a lookup in your table of z-values and decide what color to return.
I have a requirement to show time-series data as layered bar chart. Is it possible with JFreeChart? Any pointers would be really helpful.
The data would be a list of: (TS, X1, X2), where I've to plot X1 for a given Timestamp (TS) and X2 would basically serve as the label for the given value of X1.
Edit: Also, for the same TS, there might exist different X1 values. The idea is to denote all these X1 values as layered bars against the same TS.
Here's somewhat of an example of what I want:
.
(so instead of category, I'll have TS in X-axis)
It sounds like you want a BarChart (with x-axis determined by time) with the bars labelled with their values. You don't need to add a new data series for the labels, but modify the rendering of the plot.
Here's a simple example:
public class LabelledBarChartTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, "Series", new Integer(2010));
dataset.addValue(20.0, "Series", new Integer(2011));
dataset.addValue(30.0, "Series", new Integer(2012));
JFreeChart chart = ChartFactory.createBarChart(null,null,null,dataset,
PlotOrientation.VERTICAL,true,true,false);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = plot.getRenderer();
// label the points
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2);
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(
StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT, format, format);
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);
frame.setContentPane(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
}
Credit where credit is due - I got the labelling example from this example.
I'm currently trying to use JFreeChart to represent 3D data in a 2D graph.
Essentially, I have a 2d array called data[i][j]. The i and j represent the y and x coordinates where I want to plot. The value of data[i][j] represents a frequency value, which I want to represent in the graph as a colour.
I'm not entirely sure what something like this is called, but it would look something like this:
Now I have been trying to do this using XYBlockRenderer, however I am having issues with defining the dataset. I am trying to use DefaultXYZDataset, but I'm really confused at how to even define the data here.
Can someone explain how to use the DefaultXYZDataset to accomplish such a task?
DefaultXYZDataset dataset = new DefaultXYZDataset();
Concentration.dataoutHeight = Concentration.dataout[0].length;
System.out.println(Concentration.dataoutHeight);
System.out.println(ImageProcessor.MAXCBVINT);
double[][] data = new double[3][ImageProcessor.MAXCBVINT];
for (int i = 0; i < Concentration.dataoutHeight; i++) {
for (int j = 0; j < ImageProcessor.MAXCBVINT; j++) {
data[0][j] = j;//x value
data[1][j] = i;//y value
data[2][j] = Concentration.dataout[j][i][0];//Colour
}
dataset.addSeries(i, data);
}
NumberAxis xAxis = new NumberAxis("Intensity");
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
xAxis.setLowerMargin(0.0);
xAxis.setUpperMargin(0.0);
NumberAxis yAxis = new NumberAxis("Distance to Closest Blood Vessel (um)");
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yAxis.setLowerMargin(0.0);
yAxis.setUpperMargin(0.0);
XYBlockRenderer renderer = new XYBlockRenderer();
PaintScale scale = new GrayPaintScale(0, 10000.0);
renderer.setPaintScale(scale);
renderer.setBlockHeight(1);
renderer.setBlockWidth(1);
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinePaint(Color.white);
JFreeChart chart = new JFreeChart("Surface Plot", plot);
chart.removeLegend();
chart.setBackgroundPaint(Color.white);
ChartFrame frame = new ChartFrame("Surface Map - "
+ (Concentration.testing ? "TESTING using "
+ Concentration.testfile : currentFile.getName()), chart);
frame.pack();
frame.setVisible(true);
You have two options:
Represent them as 3d
3D Lib for JFreeChart
You need to use the class : XYBlockRenderer which does exactly what you are asking. You can download the JFreeChart demo collection where the code for this is given.
(source code of class here)
There is also this full code example with 4D very similar.
Can someone tell me how to change samples of series color in legend in jfreechart. What I have now is small line of series color eg: I would like to have square sample of those colors. Here is an example
Can someone help me?
Ok I found the solution. At least I think. Of course there is no simple way to do this. There is now, you know, setShape(square) method, that will do the trick, at least i haven't found one.
Basicly XY chart and time chart have "line style" legend by default in contrary to bar chart for example (if has square legend by default). So I had to remove current legend and create new one with square samples of color and this new legend add to my time chart.
LegendItemCollection legend = new LegendItemCollection();
for (int i = 0; i < seriecCount; ++i) {
chart.getXYPlot().getRenderer().setSeriesPaint(i, colorPalette.get(i));
LegendItem li = new LegendItem(data.getSeriesName(i), "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, colorPalette.get(i));
legend.add(li);
}
chart.getXYPlot().setFixedLegendItems(legend);
Thanks for attention. I hope it will help someone.
Generating your own legend, as you do above, is a perfectly acceptable way of doing things in JFreeChart. If you didn't want to do it, you can also define your own renderer with the lookupLegendShape() method overridden.
thePlot.setRenderer(new XYLineAndShapeRenderer()
{
public Shape lookupLegendShape(int series)
{
return new Rectangle(15, 15);
}
});
If you use a XYBarRenderer Class XYBarRenderer
(Subclasses: ClusteredXYBarRenderer, StackedXYBarRenderer)
You can use XYBarRenderer.setLegendBar(java.awt.Shape bar);
See: Javadoc
to get nice squares.
Example:
JFreeChart chart = ChartFactory.createXYBarChart(/*...*/);
XYPlot plot = (XYPlot) chart.getPlot();
ClusteredXYBarRenderer renderer = new ClusteredXYBarRenderer();
renderer.setLegendBar(new Rectangle(17, 17));
plot.setRenderer(renderer);