Create Fatter Candlesticks in JFreeChart - jfreechart

I'm developing an app that displays daily financial data, and have chosen to use JFreeChart. I was able to learn how to create a candlestick chart, but my problem lies in customization.
You see, what I'm aiming for is more along the lines of
While, so far all I've been able to manage is
.
No matter how far I zoom in, the candlesticks do not increase in width.
I'm fairly certain that somehow the thin candlesticks have something to do with being bound to a certain time range.. I've tried to remedy that but am not sure what I'm doing wrong here.
SSCE
public void showStockHistory(OHLCDataset dataset, String stockName) {
JFreeChart candleChart = ChartFactory.createCandlestickChart("History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setUpperMargin(0.0D);
range.setLowerMargin(0.0D);
range.setAutoRange(true);
range.setAutoRangeIncludesZero(false);
ChartPanel chartPanel = new ChartPanel(candleChart);
chartPanel.setMouseWheelEnabled(true);
chartPanel.setMouseZoomable(true);
getViewport().add(chartPanel);
}

Although my given example seems to have to no differing method calls from the demo in the first picture's code above, it nevertheless only shows thin candlesticks. I assume this to be some kind of bug.
However, I was able to rectify the issue as follows:
getting the renderer for the chart,
casting it to a type of CandlestickRenderer, and
setting its setAutoWidthMethod() method to CandlestickRenderer.WIDTHMETHOD_SMALLEST.
This is how you do it:
JFreeChart candleChart = ChartFactory.createCandlestickChart(
"History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer();
renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);

Related

Devexpress: how to add points to Chart Control at runtime?

i'm trying to build a telemetry software using Winform and Devexpress library. Specifically i'm working on a Line Chart Control and what i would like to do is to configure the chart so that it is able to display a data changing in real time.
The graph is generated reading some external sensors that send informations at a rate of 10 values per second.
This is my code for initialize the chart:
series1 = new Series("test test", ViewType.Line);
chartControl1.Series.Add(series1);
series1.ArgumentScaleType = ScaleType.Numerical;
((LineSeriesView)series1.View).LineMarkerOptions.Kind = MarkerKind.Triangle;
((LineSeriesView)series1.View).LineStyle.DashStyle = DashStyle.Dash;
((XYDiagram)chartControl1.Diagram).EnableAxisXZooming = true;
chartControl1.Legend.Visibility = DefaultBoolean.False;
chartControl1.Titles.Add(new ChartTitle());
chartControl1.Titles[0].Text = "A Line Chart";
chartControl1.Dock = DockStyle.Fill;
And this is the one that add a new point and remove the first point available so that the amount of points in my chart is always the same (after a minimum amount of points is reached) and it keeps updating itself displaying the last X seconds of values and discarding the old values.
series1.Points.RemoveRange(0, 1);
series1.Points.Add(new SeriesPoint(time, value));
...
AxisXRange.SetMinMaxValues(newFirstTime, time);
AxisRange is the following
Range AxisXRange
{
get
{
SwiftPlotDiagram diagram = chartControl1.Diagram as SwiftPlotDiagram;
if (diagram != null)
return diagram.AxisX.VisualRange;
return null;
}
}
**The problem ** is that this code works temporarily. After some seconds, the chart stop working and a big red cross is displayed over it.
Is there something that i'm missing with its configuration?
Do you know any better way to realize my task?
Any help would be appreciated.
Thank you
I think you doing it nearly right. Devexpress has an Article about RealTime-Charts. They doing it the same way but using a timer for updating the data. Maybe this would fix your painting problems.

JFreeChart - Determine the moment when chart is not visible because of big amount of range axes

I am using JFreeChart library to create Chart. I need to present big amount of data on the same chart. Because of that I have many range axes descriptions.
Unfortunately when there are too many range axes, chart is no longer visible. It is possible to make chart visible by calling this simple lines (Where plot is an XYPlot instance):
int axises = plot.getRangeAxisCount();
for (int i = 0; i < axises; i++) {
plot.getRangeAxis(i).setVisible(false);
}
This simple portion of code hides all RangeAxes. After that code execution, chart does not have any description for range axes but it is visible.
But unfortunately I am unable to figure out how can I determine does the chart is visible or not during the processing time.
Information that is important to me is in:
chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth()
But unfortunately I am performing many operations on the chart (for example zoom, move, etc.) and because of that I need to have this information everytime, when state of the chart changes. I am unable to take that information whenever plotChanged() method of the PlotChangeListener interface is called, because there is no plot (this event is not fired). chartChanged() method from ChartChangeListener is fired too early - chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth() returns old values. Any ideas?
You can add a ChartProgressListener to the ChartPanel.
chart.addProgressListener(new ChartProgressListener() {
#Override
public void chartProgress(ChartProgressEvent event) {
System.out.println(event.getType() + " "
+ event.getPercent() + " "
+ chartPanel.getChartRenderingInfo()
.getPlotInfo().getDataArea().getWidth());
}
});

how to display data dynamically in jfreechart

I have created a function that processes some value.
Now, my problem is that the graph i create displays the values generated after ALL the values have been calculated. i want to generate the graph such that the as and when the data is calculated; it must be plotted on the data.
The calculation and the display of data in the graph must appear parallel.
I have generated the graph as:
dataset.addSeries(series1);
dataset = new XYSeriesCollection();
series1 = new XYSeries("% ERROR");
chart = ChartFactory.createXYLineChart("Percent Error", "Number of Records", "% Error", dataset,PlotOrientation.VERTICAL,true, true, true);
chart.setBackgroundPaint(Color.WHITE);
frame = new ChartFrame("Error graph", chart);
frame.pack();
frame.setVisible(true);
The series1 is plotted on the graph.the values in series1 is added on every iteration of the loop which generates some value.
Can it be done using wait and notify? I am not really sure about this functions. Please help
The add() method of XYSeries "Adds a data item to the series and sends a SeriesChangeEvent to all registered listeners." One approach is to invoke add() using javax.swing.Timer, as shown in this example.
If your data are computed within a fix period what about using DinamycTimeSeriesCollection?
They are very usefull.
You can find an example in this question where I managed the problem of having periods multiple of a millisecond.

jFreeChart: DateAxis to behave like a CategoryAxis. Only discrete datevalues to be printed

I have a TimeSeries Chart with a XYdataset which has the milliseconds of each dates.
Now I want to have a chart that only displays each date one time on the axis, no matter what the range of dates is. I already got it so far, that only the date is printed, not the time (via setNumberFormatOverride() method).
What I have now, is this:
alt text http://dl.getdropbox.com/u/1144075/Bildschirmfoto%202010-08-05%20um%2016.51.39.JPG
What I want, is this:
alt text http://dl.getdropbox.com/u/1144075/Bildschirmfoto%202010-08-05%20um%2016.54.54.JPG
This is a snippet of my code. also, the setLabelAngle() method don't work?!
JFreeChart chart = ChartFactory.createTimeSeriesChart(...);
XYPlot xyplot = (XYPlot)chart.getPlot();
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setLabelAngle(Math.PI / 6.0);`
numberaxis.setNumberFormatOverride((DecimalFormat)DecimalFormat.getInstance(Locale.GERMAN));
You can set the TickUnit to any convenient DateTickUnitType. I use DAY in this example and MMM to verify the Locale:
axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1,
new SimpleDateFormat("dd.MMM.yy", Locale.GERMAN)));
As you are a returning customer, I'll address the second question, too: setLabelAngle() works perfectly well, but it changes the axis label in the chart legend. Using setVerticalTickLabels() is an alternative for the tick labels themselves:
axis.setVerticalTickLabels(true);

jFreeChart: Possible to combine StatisticalBarRenderer and LayeredBarRenderer?

I want to create a chart that consists of couples of two bars.
one of them a regular bar, the second one is layered and has a deviation.
This is a snippet of my code:
private static JFreeChart createStatisticalBarChart(DefaultStatisticalCategoryDataset dataset, String chartTitle, String domainAxisLabel, String rangeAxisLabel, List<String> sunndayMap) {
CategoryAxis xAxis = new CategoryAxis(domainAxisLabel);
ValueAxis yAxis = new NumberAxis(rangeAxisLabel);
CategoryItemRenderer renderer = new StatisticalBarRenderer();
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(chartTitle,
new Font("Arial", Font.PLAIN, 10),
plot,
true);
My dataset has mean+standarddeviatin records with every second record's deviation being "0", so that no range indicators will be displayed.
How can I accomplish that?
You can use a small negative value in setItemMargin() to achieve an overlap, for example
renderer.setItemMargin(-0.10f);
It appears that standard deviation lines are not drawn if the getStdDevValue() method returns null. You might try that value instead of "0".

Resources