Http://blog.sina.com.cn/s/blog_4ad042e50100q7d9.html
Complete solution for Stock candlestick chart drawing with Jfreechart(2011-04-30 13:27:17)
Tags: drawing stock K- line it |
Category: Software _software |
Because of the need to work, contact some of the stock graph drawing class work, one of the most important is the stock chart of the drawing, if the use of programming language at the bottom of the graphic drawing method to draw such graphics, if the programming language is not particularly familiar with, generally there is a great difficulty, through the search on the internet found that Take two of the most familiar languages, including the PHP version of the open source Jpgraph drawing program and the Java version of the Jfreechart Drawing Toolkit, Jpgraph used for a while, but found that the graphic details of the processing is not delicate and delicate, Finally turn to use Jfreechart, this is a foreign open source graphics drawing Java package, inside of the drawing function is quite strong, more main use Java's powerful object-oriented function, can make many aspects of customization and modification.
There are a lot of people on the Internet asking how to use Jfreechart to draw stock chart, by reading some of the examples, most of the messy, did not give a match with the Chinese stock market example, because the color of the Chinese stock market candlestick chart is a little different from foreign countries, There are many people have encountered how to remove the Saturday, Sunday such non-existence of the date of the transaction, there is no satisfactory answer online, through the practice, I compiled a complete example, give a complete comment, as long as the appropriate classpath in eclipse, and even do not modify it can be run, Get a combination of daily candlestick and daily turnover, hope to provide you with a little help, but also hope that we can communicate together, improve and dig more features!
Before the source code is explained, it is necessary to describe some of the concepts I have learned about Jfreechart, the Jfreechart tool basically divides a graph into several logical parts.
1. First, the entire Canvas object (chart), the canvas is quite similar to a large container, containing some graphic elements, such as a blank sheet of paper
2. Then the Paint area object (plot), which is the area occupied by the actual graph, such as a rectangle on a white paper
3. Then the graph requires two axis objects (axis), such as two axes on a white paper
4. Finally, how to make an image of the object (render), such as a drawing pen
5. In addition to these objects above, one of the most important is to generate graphs from the required datasets (dataset)
As for some specific object details, we have to ask you to read the relevant API documentation, here is not detailed description
Package msTest;
Import java.awt.color;//Color System
Import java.text.simpledateformat;//Time format
Import java.awt.paint;//Brush System
Import org.jfree.data.time.*;
Import Org.jfree.data.time.Day;
Import org.jfree.data.time.ohlc.OHLCSeries;
Import org.jfree.data.time.ohlc.OHLCSeriesCollection;
Import org.jfree.chart.renderer.xy.*;
Import org.jfree.chart.axis.*;
Import org.jfree.chart.plot.*;
Import org.jfree.chart.*;
public class Klinecombinechart {
public static void Main (string[] args) {
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");//Set Date format
Double Highvalue = double.min_value;//Sets the maximum value in the candlestick data
Double MinValue = double.max_value;//Sets the minimum value of the candlestick data
Double High2value = double.min_value;//Sets the maximum value of the volume
Double min2value = double.max_value;//Set minimum value for volume
Ohlcseries series = new Ohlcseries ("");//High Open Low collection data series, stock candlestick chart four data, in turn,
Series.add (New Day (28, 9, 2007), 9.2, 9.58, 9.16, 9.34);
Series.add (New Day (27, 9, 2007), 8.9, 9.06, 8.83, 8.96);
Series.add (New Day (26, 9, 2007), 9.0, 9.1, 8.82, 9.04);
Series.add (New Day (25, 9, 2007), 9.25, 9.33, 8.88, 9.00);
Series.add (New Day (24, 9, 2007), 9.05, 9.50, 8.91, 9.25);
Series.add (New Day (21, 9, 2007), 8.68, 9.05, 8.40, 9.00);
Series.add (New Day (20, 9, 2007), 8.68, 8.95, 8.50, 8.69);
Series.add (New Day (19, 9, 2007), 8.80, 8.94, 8.50, 8.66);
Series.add (New Day (18, 9, 2007), 8.88, 9.17, 8.69, 8.80);
Series.add (New Day (17, 9, 2007), 8.26, 8.98, 8.15, 8.89);
Series.add (New Day (14, 9, 2007), 8.44, 8.45, 8.13, 8.33);
Series.add (New Day (13, 9, 2007), 8.13, 8.46, 7.97, 8.42);
Series.add (New Day (12, 9, 2007), 8.2, 8.4, 7.81, 8.13);
Series.add (New Day (11, 9, 2007), 9.0, 9.0, 8.1, 8.24);
Series.add (New Day (10, 9, 2007), 8.6, 9.03, 8.40, 8.95);
Series.add (New Day (7, 9, 2007), 8.89, 9.04, 8.70, 8.73);
Series.add (New Day (6, 9, 2007), 8.4, 9.08, 8.33, 8.88);
Series.add (New Day (5, 9, 2007), 8.2, 8.74, 8.17, 8.36);
Series.add (New Day (4, 9, 2007), 7.7, 8.46, 7.67, 8.27);
Series.add (New Day (3, 9, 2007), 7.5, 7.8, 7.48, 7.69);
Series.add (New Day (31, 8, 2007), 7.4, 7.6, 7.28, 7.43);
Series.add (New Day (30, 8, 2007), 7.42, 7.56, 7.31, 7.40);
Series.add (New Day (29, 8, 2007), 7.42, 7.66, 7.22, 7.33);
Series.add (New Day (28, 8, 2007), 7.31, 7.70, 7.15, 7.56);
Series.add (New Day (27, 8, 2007), 7.05, 7.46, 7.02, 7.41);
Series.add (New Day (24, 8, 2007), 7.05, 7.09, 6.90, 6.99);
Series.add (New Day (23, 8, 2007), 7.12, 7.16, 7.00, 7.03);
Series.add (New Day (22, 8, 2007), 6.96, 7.15, 6.93, 7.11);
Series.add (New Day (21, 8, 2007), 7.10, 7.15, 7.02, 7.07);
Series.add (New Day (20, 8, 2007), 7.02, 7.19, 6.94, 7.14);
Final Ohlcseriescollection SeriesCollection = new Ohlcseriescollection ();//Data set that retains the candlestick data must be declared final, followed by an anonymous inner class.
Seriescollection.addseries (series);
TimeSeries series2=new TimeSeries ("");//Correspondence Time Volume data
Series2.add (New Day (28, 9, 2007), 260659400/100);
Series2.add (New Day (27, 9, 2007), 119701900/100);
Series2.add (New Day (26, 9, 2007), 109719000/100);
Series2.add (New Day (25, 9, 2007), 178492400/100);
Series2.add (New Day (24, 9, 2007), 269978500/100);
Series2.add (New Day (21, 9, 2007), 361042300/100);
Series2.add (New Day (20, 9, 2007), 173912600/100);
Series2.add (New Day (19, 9, 2007), 154622600/100);
Series2.add (New Day (18, 9, 2007), 200661600/100);
Series2.add (New Day (17, 9, 2007), 312799600/100);
Series2.add (New Day (14, 9, 2007), 141652900/100);
Series2.add (New Day (13, 9, 2007), 221260400/100);
Series2.add (New Day (12, 9, 2007), 274795400/100);
Series2.add (New Day (11, 9, 2007), 289287300/100);
Series2.add (New Day (10, 9, 2007), 289063600/100);
Series2.add (New Day (7, 9, 2007), 351575300/100);
Series2.add (New Day (6, 9, 2007), 451357300/100);
Series2.add (New Day (5, 9, 2007), 442421200/100);
Series2.add (New Day (4, 9, 2007), 671942600/100);
Series2.add (New Day (3, 9, 2007), 349647800/100);
Series2.add (New Day (31, 8, 2007), 225339300/100);
Series2.add (New Day (30, 8, 2007), 160048200/100);
Series2.add (New Day (29, 8, 2007), 247341700/100);
Series2.add (New Day (28, 8, 2007), 394975400/100);
Series2.add (New Day (27, 8, 2007), 475797500/100);
Series2.add (New Day (24, 8, 2007), 297679500/100);
Series2.add (New Day (23, 8, 2007), 191760600/100);
Series2.add (New Day (22, 8, 2007), 232570200/100);
Series2.add (New Day (21, 8, 2007), 215693200/100);
Series2.add (New Day (20, 8, 2007), 200287500/100);
Timeseriescollection timeseriescollection=new timeseriescollection ();//set of reserved volume data
Timeseriescollection.addseries (Series2);
Get the highest and lowest values for the candlestick data
int seriescount = Seriescollection.getseriescount ();//Total number of sequences, present as a
for (int i = 0; i < Seriescount; i++) {
int itemCount = Seriescollection.getitemcount (i);//number of data items per sequence
for (int j = 0; J < ItemCount; J + +) {
if (Highvalue < Seriescollection.gethighvalue (i, J)) {//Take the maximum value of the first J data item in the sequence I
Highvalue = Seriescollection.gethighvalue (i, j);
}
if (MinValue > Seriescollection.getlowvalue (i, J)) {//Take the minimum value of the first J data item in the sequence I
MinValue = Seriescollection.getlowvalue (i, j);
}
}
}
Get the highest and lowest values
int seriesCount2 = Timeseriescollection.getseriescount ();//Total number of sequences, present as a
for (int i = 0; i < SeriesCount2; i++) {
int itemCount = Timeseriescollection.getitemcount (i);//number of data items per sequence
for (int j = 0; J < ItemCount; J + +) {
if (High2value < Timeseriescollection.getyvalue (i,j)) {//Take the value of the J data item in the first sequence
High2value = Timeseriescollection.getyvalue (i,j);
}
if (Min2value > Timeseriescollection.getyvalue (i, J)) {//Take the value of the first J data item in the sequence I
Min2value = Timeseriescollection.getyvalue (i, j);
}
}
}
Final Candlestickrenderer candlestickrender=new candlestickrenderer ()///Set the drawing of the candlestick chart, must be declared final, followed by the anonymous inner class inside the
Candlestickrender.setuseoutlinepaint (TRUE); Sets whether to use custom border lines, the color of the program's own border line is not in line with China's stock market habits
Candlestickrender.setautowidthmethod (candlestickrenderer.widthmethod_average);//Set how the width of the candlestick chart is set
Candlestickrender.setautowidthgap (0.001);//Set the interval between each candlestick
Candlestickrender.setuppaint (color.red);//Set the chart color for stock rise
Candlestickrender.setdownpaint (Color.green);//Set the chart color for stock fall
Dateaxis x1axis=new Dateaxis ();//Set X-axis, which is the timeline
X1axis.setautorange (false);//setting does not take the automatic setting of the time range
try{
X1axis.setrange (Dateformat.parse ("2007-08-20"), Dateformat.parse ("2007-09-29"));//Set the time range, notice that the maximum value of the time is more than the maximum value of the existing time of day
}catch (Exception e) {
E.printstacktrace ();
}
X1axis.settimeline (Segmentedtimeline.newmondaythroughfridaytimeline ());//Set the rules for the timeline display, and use this method to exclude dates that were not traded for Saturday and Sunday ( A lot of people don't know about this method) so that the graphic looks continuous
X1axis.setautotickunitselection (false);//setting does not take automatic selection of scale values
X1axis.settickmarkposition (Datetickmarkposition.middle);//Setting the location of the marker
X1axis.setstandardtickunits (Dateaxis.createstandarddatetickunits ());//Set a standard timescale unit
X1axis.settickunit (New Datetickunit (datetickunit.day,7));//Set the interval for the timescale, typically in weeks
X1axis.setdateformatoverride (New SimpleDateFormat ("Yyyy-mm-dd"));//Set the format of the display time
Numberaxis y1axis=new Numberaxis ();//Set y-axis, which is the digital axis
Y1axis.setautorange (false);//Do not use auto set range
Y1axis.setrange (minvalue*0.9, highvalue*1.1);//Set the range of y-axis values to be lower than the lowest value, a little larger than the maximum, so that the graphic looks beautiful
Y1axis.settickunit (New Numbertickunit ((highvalue*1.1-minvalue*0.9)/10);//Set the density of the scale display
Xyplot plot1=new Xyplot (seriescollection,x1axis,y1axis,candlestickrender);//Set Paint area objects
Xybarrenderer xybarrender=new Xybarrenderer () {
Private static final Long Serialversionuid = 1l;//to avoid warning messages, set this value
Public Paint getitempaint (int i, int j) {//anonymous inner class used to process the same day's Volume column chart color consistent with the chart's color
if (Seriescollection.getclosevalue (i,j) >seriescollection.getopenvalue (i,j)) {//close price is higher than open price, stock rises, choose the color of stock rising
return Candlestickrender.getuppaint ();
}else{
return Candlestickrender.getdownpaint ();
}
}};
Xybarrender.setmargin (0.1);//Set the interval between column charts
Numberaxis y2axis=new Numberaxis ()///Set y-axis, value, subsequent settings, refer to the y-axis settings above
Y2axis.setautorange (FALSE);
Y2axis.setrange (min2value*0.9, high2value*1.1);
Y2axis.settickunit (New Numbertickunit ((high2value*1.1-min2value*0.9)/4));
Xyplot plot2=new Xyplot (timeseriescollection,null,y2axis,xybarrender);//Create a second Paint area object, where the x-axis is set to a null value, Because you want to share the x-axis with the first Paint area object
Combineddomainxyplot Combineddomainxyplot = new Combineddomainxyplot (x1axis);//Create an appropriate joint graphics area object with the X axis as the shared axis
Combineddomainxyplot.add (PLOT1, 2);//Add a graphics area object, followed by a number that calculates how large the area object should occupy 2/3
Combineddomainxyplot.add (Plot2, 1);//Add a graphics area object, followed by a number that calculates how large the area object should occupy 1/3
Combineddomainxyplot.setgap (10);//Set the space between two graphics area objects
Jfreechart chart = new Jfreechart ("China Unicom", Jfreechart.default_title_font, Combineddomainxyplot, false);
Chartframe frame = new Chartframe ("China Unicom Stock", chart);
Frame.pack ();
Frame.setvisible (TRUE);
}
}
In order to be able to ensure that the upper shadow line and the bottom line of the candlestick chart can be consistent with the color of the candlestick chart itself, find a lot of method settings are unsuccessful, it can be said that currently does not provide a separate way to draw a candle border color, so, after my experiment, the source code of which was modified, The specific changes are in the download back of the document Source\org\jfree\chart\renderer\xy\candlestickrenderer.java file, will
Paint p = Getitempaint (series, item);
Paint outlinepaint = null;
Outlinepaint = Getitemoutlinepaint (series, item);
//}
//
Change to the following code, this will ensure that the upper shadow line and the lower shadow of the candlestick chart can be consistent with the color of the candlestick chart itself, in line with the Chinese stock market candlestick pattern Color habits:
Paint p = Getitempaint (series, item);
Paint outlinepaint = null;
if (this.useoutlinepaint) {
if (Yclose>yopen) {
if (this.uppaint! = null) {
Outlinepaint=this.uppaint;
// }
else {
outlinepaint=p;
// }
}else{
if (this.downpaint! = null) {
Outlinepaint=this.downpaint;
// }
else {
outlinepaint=p;
// }
// }
// }
Complete solution for Stock candlestick chart drawing with Jfreechart