Reprint Please specify the live: Http://blog.csdn.net/andywuchuanlong
Now in the hand is a security information type of app, which involves the stock market interface, there are candlestick charts in the market, see many people on the Internet to seek this information, so I specifically wrote a demo here to share with you.
Here's what it does:
This interface is how to draw out I do not introduce, we can download the project source code.
The background map is the use of canvas first draw a rectangle, and then draw a few dashed lines, the average figure is drawn through path, in short, the drawing is very simple, I do not introduce here, we can go to GitHub download source to see. The concept of moving averages, highs, lows, closing prices, open prices can be Baidu a bit.
What I'm going to introduce here is the computational problem:
You can see the time-sharing chart, the day K, the month K of the left of the transaction price is not the same, and our K line is drawn through this price, that is, the price is the moment of change, then our K-line drawing is also a change. Suppose we want to calculate how the minute that the price is 25.69 in the tick chart should be drawn, where to draw in the screen, then this should be how to draw, the price is variable, the position of the painting is also a change, but a little bit of our screen size is unchanged. So we can use the height of the background map to calculate where the line graph of a price should begin. We can figure out how many prices a pixel corresponds to, and the analysis chart is as follows:
The price and pixel form a proportional calculation is: double heightscale = (endy-starty)/(Highprice-lowprice);
So the price of 25.69 should be drawn in Mstarty = (float) (starty+ (Highprice- 25.69) * heightscale);
This understanding of the other principles are the same, I do not introduce, the following is part of the code:
< Span style= "COLOR: #666666" >
@Overrideprotected void Drawkchatbackground () {rect dirty = new Rect (left, Kcharttop, right, kchartbottom);//Rectangle drawing of the background map McAn Vas.drawrect (Dirty, linegraypaint); Patheffect effects = new Dashpatheffect (new float[] {5, 5, 5, 5}, 1); Linegraypaint.setpatheffect (effects); Path PATH = new path (), int y = kcharttop + 15;//Draw the dashed line above Path.moveto (left, y);p Ath.lineto (right, y); String Text = Getpricetext (highprice), int textHeight = (int) (Textgraypaint.descent ()-textgraypaint.ascent ()); Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + TEXTHEIGHT/2, textgraypaint);d ouble max = HighPrice -Lowprice;if (Max > 10) {//divided into four equal//draw the middle of three dashed int n = 4;double sper = (highprice-lowprice)/4;//The price of each division represents for (int i=1; i<n;i++) {y = i* ((kchartbottom-kcharttop)/N) + Kcharttop;path.moveto (left, y);p Ath.lineto (right,y); text = Getpricet Ext (highprice-i*sper); Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + TEXTHEIGHT/2, Textgraypaint);}} else{//divided into two halves//draw the middle of the dotted line y = (KchartbottOm-kcharttop)/2 + Kcharttop;path.moveto (left, y);p Ath.lineto (right, y); text = Getpricetext (Highprice-(HIGHPRICE-LOWP Rice)/2); Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + textheight/2,textgraypaint);} Draw the dotted line below y = Kchartbottom-15;path.moveto (left, y);p Ath.lineto (right, y); text = Getpricetext (lowprice); Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + textheight/2,textgraypaint)////draw the dotted line and the following date for (int i = num-1; i > 0; i--) {int x = left + perwidth * I;path.moveto (x, Kcharttop);p Ath.lineto (x, Kchartbottom);p erxpoint[i-1] = x;} Mcanvas.drawpath (path, linegraypaint);}
@Overrideprotected void Drawmachart () {//moving average Path path5 = new Path (); Path path10 = new Path (); Path PATH20 = new Path ();d ouble Heightscale = (kchartbottom-kcharttop)/(highprice-lowprice); int mastart = left;float m Astarty;path5.moveto (Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE5 ()) * Heightscale)); Path10.moveto (Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE10 ()) * Heightscale));p Ath20.moveto ( Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE20 ()) * Heightscale)); for (Singlestockinfo Info:infos) {Mastart + = per * perhalf;//actual data per day is 4/6, left and right margin each 1/6 mastarty = (float) (Kcharttop + (Highprice-info.getmavalue5 ()) * He Ightscale);p Ath5.lineto (Mastart, mastarty); mastarty = (float) (Kcharttop + (Highprice-info.getmavalue10 ()) * Heightscale);p Ath10.lineto (Mastart, mastarty); mastarty = (float) (Kcharttop + (HIGHPRICE-INFO.GETMAVALUE20 ()) * Heightscale);p Ath20.lineto (Mastart, mastarty); Mastart + = per * perhalf;} Paint paint = new PainT ();p Aint.setcolor (color.blue);p Aint.setantialias (True);p Aint.setstrokewidth (2);p Aint.setstyle (Style.stroke); Mcanvas.drawpath (PATH5, paint);p Aint.setcolor (Color.magenta), Mcanvas.drawpath (path10, paint);p Aint.setcolor ( Color.green); Mcanvas.drawpath (path20, paint);}
/** * The following column chart */@Overrideprotected void Drawpillarschart (int flag) {linegraypaint.setpatheffect (null); Rect dirty = new Rect (left, Pillarscharttop, right, pillarschartbottom);//Rectangle mcanvas.drawrect of the background map (dirty, Linegraypaint ); int y = pillarscharttop + (pillarschartbottom-pillarscharttop)/2;mcanvas.drawline (Left,y,right, y, LineGrayPaint); /median value string totalcount = Getpricetext (maxcount/2/10000); float maginleft = Left-textgraypaint.measuretext (TotalCount) -5;mcanvas.drawtext (TotalCount, Maginleft, y,textgraypaint);//value above TotalCount = Getpricetext (maxcount/10000); Maginleft = Left-textgraypaint.measuretext (totalcount)-5;mcanvas.drawtext (TotalCount, MaginLeft, PillarsChartTop, Textgraypaint);//the value below TotalCount = "million hands"; maginleft = Left-textgraypaint.measuretext (totalcount)-5;mcanvas.drawtext ( TotalCount, Maginleft, pillarschartbottom,textgraypaint); int pstart = left;float pstarty;double HeightScale = ( Pillarschartbottom-pillarscharttop)/maxcount; Paint paint = new paint ();p Aint.setantiaLias (True);p Aint.setstyle (Paint.Style.FILL), if (flag = = Stockservice.flag) {for (Minuteinfo Info:minuteinfos) {Pstart + = per * per16;//The actual data for each day is 4/6, plus 1/6pstarty = (float) (Pillarscharttop + (Maxcount-info.getvolume ()) * Heightscale);d I Rty = new Rect (Pstart, (int) pstarty, (int) (Pstart + per * per46), pillarsChartbottom-2);p Aint.setcolor (Info.getcolor ()); Draw a rectangle Mcanvas.drawrect (dirty, paint);p start + = per * per56;//to the right of the pitch 5/6}}else{for (Singlestockinfo Info:infos) { Pstart + = per * per16;//The actual data for each day is 4/6, plus 1/6pstarty = (float) (Pillarscharttop + (Maxcount-info.gettotalcount ()) * Heig Htscale);d irty = new Rect (Pstart, (int) pstarty, (int) (Pstart + per * per46), pillarsChartbottom-2);p Aint.setcolor (info.g Etcolor ());//Draw the background image of the rectangle Mcanvas.drawrect (dirty, paint);p start + = per * per56;//to the right of the pitch 5/6}}
/** * * Time chart */@Overridepublic void Drawhourschart () {Double Heightscale = (kchartbottom-kcharttop)/(highprice-lowprice); I NT cleft = Left;int ctop = 0; Path PATH = new Path ();p Ath.moveto (cleft, KChartbottom-2); int position = 0;int Perpointx = perxpoint[position];//Record First vertical virtual The x-coordinate of the line for (Minuteinfo Info:minuteinfos) {cleft + = per * Per16;ctop = (int) (Kcharttop + (Highprice-info.getnow ()) * HeightS Cale);p Ath.lineto (cleft + per * per26, ctop); if (cleft >= perpointx) {//Just draw to the first vertical dashed line where you need to draw the following time string text = Kchartutil . Getminute (Info.getminute ()); float textWidth = Textgraypaint.measuretext (text); int textHeight = (int) ( Textgraypaint.descent ()-textgraypaint.ascent ()) Mcanvas.drawtext (text, PERPOINTX-TEXTWIDTH/2, KChartbottom + TextHeight, Textgraypaint); if (! ( Position = = perxpoint.length-1) {log.e (TAG, perpointx+ "----------" +info.getminute () + "---" +text);p Erpointx = Perxpoint[++position];}} Cleft + = per * per56;//to the right of the pitch 5/6}path.lineto (cleft, KChartbottom-2); Paint Linepaint = new paint (); LinePaint.setcolor (Color.Blue); Linepaint.setantialias (TRUE); Linepaint.setstrokewidth (1); Linepaint.setstyle (Style.stroke);//linepaint.setstyle (Style.stroke); Mcanvas.drawpath (path, LinePaint); Linepaint.setalpha (50); Linepaint.setstyle (Style.fill); Mcanvas.drawpath (path, linepaint);}
GitHub Address: Https://github.com/chuanlongwu/kChart4Android
Stock Quotes chart for Android version