ImgHelper(XStock)

默认 2011-01-12 16:20

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using XStock.ClassLibrary.Entity;

namespace XStock.ClassLibrary.Chart
{
   public class ImgHelper
   {

       int WIDTH = 300;

       int HEIGHT = 500;

       const int EDGE = 100;

       /// <summary>
       /// 放大的倍数,最少100
       /// </summary>
       const int MULTIPLE=200;

       /// <summary>
       /// 间隔
       /// </summary>
       const int INTERVAL = 10;

       Bitmap bitmap = null;
       Graphics graphics = null;
       decimal Max = decimal.MinValue;
       decimal Min = decimal.MaxValue;

       List<StackSingleInfo> list = null;

       public ImgHelper(List<StackSingleInfo> list)
       {

           this.list = list;

           this.list.Sort((a, b) => a.Date.CompareTo(b.Date));

           foreach (var item in list)
           {
               if (Max < item.High * MULTIPLE)
                   Max = item.High * MULTIPLE;
               if (Min > item.Low * MULTIPLE)
                   Min = item.Low * MULTIPLE;
           }
           Max += EDGE;
           if (Min > EDGE)
               Min -= EDGE;
           this.HEIGHT = (int)(Max - Min);


           this.WIDTH = list.Count * INTERVAL + EDGE * 2;


           bitmap = new Bitmap(WIDTH, HEIGHT);

           graphics = Graphics.FromImage(bitmap);

           graphics.Clear(Color.White);


           foreach (var item in this.list)
           {
               this.SetCandleLine(item);
           }

       }

       public void SetLine(DateTime start, decimal price1, DateTime end, decimal price2)
       {
           graphics.SmoothingMode = SmoothingMode.AntiAlias;
           graphics.DrawLine(new Pen(Color.Black), X(start), Y(price1), X(end), Y(price2));
       }
       public void SetTestLine(DateTime start, decimal price1, DateTime end, decimal price2,Color color)
       {
           graphics.SmoothingMode = SmoothingMode.AntiAlias;
           graphics.DrawLine(new Pen(color), X(start)+4, Y(price1), X(end)+4, Y(price2));
       }


       public void Save(string path)
       {
           bitmap.Save(path, ImageFormat.Jpeg);
       }


       private float X(DateTime date)
       {
           var find = list.Find(a => a.Date == date);

           int index = list.IndexOf(find);
           int x = EDGE + index * INTERVAL;
           return x;
       }

       public float Y(decimal price)
       {
           return HEIGHT - (int)(price * MULTIPLE - Min);
       }


       /// <summary>
       /// 设置label
       /// </summary>
       /// <param name="item"></param>
       /// <param name="SetLabelLastIsUp"></param>
       public void SetLabel(StackSingleInfo item, bool SetLabelLastIsUp)
       {
           decimal offset = 0.2M;
           decimal offsetBasic = 0.05M;

           decimal offsetSecondLine = 0.03M;

           graphics.SmoothingMode = SmoothingMode.AntiAlias;

           if (SetLabelLastIsUp)
           {
               graphics.DrawLine(new Pen(Color.Blue), X(item.Date), Y(item.High + offsetBasic), X(item.Date), Y(item.High + offset + offsetBasic));
               graphics.DrawString(item.Close.ToString("#.##"), new Font(new FontFamily("宋体"), 8), Brushes.Black, X(item.Date), Y(item.High + offset + offsetBasic * 2));
               graphics.DrawString(item.Date.ToString("yyyy-MM-dd HH:mm"), new Font(new FontFamily("宋体"), 8), Brushes.Black, X(item.Date), Y(item.High + offset + offsetBasic * 2 - offsetSecondLine));
           }
           else
           {
               graphics.DrawLine(new Pen(Color.Blue), X(item.Date), Y(item.Low - offsetBasic), X(item.Date), Y(item.Low - offset - offsetBasic));
               graphics.DrawString(item.Close.ToString("#.##"), new Font(new FontFamily("宋体"), 8), Brushes.Black, X(item.Date), Y(item.Low - offset - offsetBasic * 2));
               graphics.DrawString(item.Date.ToString("yyyy-MM-dd HH:mm"), new Font(new FontFamily("宋体"), 8), Brushes.Black, X(item.Date), Y(item.Low - offset - offsetBasic * 2 - offsetSecondLine));
           }

         
       }


       /// <summary>
       /// 设置蜡烛图
       /// </summary>
       /// <param name="item"></param>
       public void SetCandleLine(StackSingleInfo item)
       {
           Color color = (item.Close > item.Open) ? Color.Red : Color.Green;
           graphics.SmoothingMode = SmoothingMode.AntiAlias;

           graphics.DrawLine(new Pen(color), X(item.Date) - 2, Y(item.HighPoint), X(item.Date) - 2, Y(item.LowPoint));
           graphics.DrawLine(new Pen(color), X(item.Date) - 1, Y(item.HighPoint), X(item.Date) - 1, Y(item.LowPoint));
           graphics.DrawLine(new Pen(color), X(item.Date), Y(item.High), X(item.Date), Y(item.Low));
           graphics.DrawLine(new Pen(color), X(item.Date) + 1, Y(item.HighPoint), X(item.Date) + 1, Y(item.LowPoint));
           graphics.DrawLine(new Pen(color), X(item.Date) + 2, Y(item.HighPoint), X(item.Date) + 2, Y(item.LowPoint));
       }


       public void SetCircle(DateTime date, decimal price)
       {
           Brush redBrush = new SolidBrush(Color.Blue);
           graphics.FillEllipse(redBrush, new Rectangle((int)X(date), (int)Y(price), 4, 4));
       }

       //public void SetSoldLine(DateTime startDate, DateTime endDate)
       //{
       //    var start = list.Find(a => a.Date == startDate);
       //    var end = list.Find(a => a.Date == endDate);
       //    graphics.SmoothingMode = SmoothingMode.AntiAlias;
       //    graphics.DrawLine(new Pen(Color.Red), X(start.Date), Y(start.High), X(end.Date), Y(end.Low));
       //}

   }
}


Leave a Reply