1. using System.Collections.Generic;
  2. namespace Chart
  3. {
  4. public class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Chart chart = new Chart();
  9. ChartType chartType = ChartType.Histogram;
  10. string path = @"..\..\JSON.json";
  11. DataSource dataSource = new JsonDataSource();
  12. List<Composition> Compositions = dataSource.GetDataList(path);
  13. chart.Compositions.AddRange(Compositions);
  14. chart.Draw(chartType);
  15. chart.Save();
  16. }
  17. }
  18. }

Program.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. namespace Chart
  6. {
  7. public class Chart
  8. {
  9. private Bitmap bmp = new Bitmap(, );
  10. List<Composition> composition = new List<Composition>();
  11. public List<Composition> Compositions { get { return composition; } }
  12. private float width;
  13.  
  14. private float Width
  15. {
  16. get
  17. {
  18. int sum = ;
  19. foreach (var composition in Compositions)
  20. {
  21. sum += composition.DataPoints.Count + ;
  22. }
  23. width = (float) / sum;
  24. return width;
  25. }
  26. }
  27.  
  28. public void Draw(ChartType chartType)
  29. {
  30. Series series;
  31.  
  32. switch (chartType)
  33. {
  34. case ChartType.LineChart:
  35. series = new LineSeries();
  36. break;
  37.  
  38. case ChartType.Histogram:
  39. series = new HistogramSeries();
  40. break;
  41.  
  42. case ChartType.PieChart:
  43. series = new PieSeries();
  44. break;
  45.  
  46. default:
  47. throw new ArgumentOutOfRangeException("Nonexistent ChartType!");
  48. }
  49.  
  50. foreach (var comPosition in Compositions)
  51. {
  52. series.Legend.Add(comPosition.Name);
  53. }
  54.  
  55. Platform platform = new Windows(bmp);
  56.  
  57. series.Draw(Width, platform, Compositions);
  58. }
  59.  
  60. public void Save()
  61. {
  62. bmp.Save(@"..\..\1.bmp");
  63. Process.Start(@"..\..\1.bmp");
  64. }
  65. }
  66. }

Chart.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. namespace Chart
  5. {
  6. public abstract class Series
  7. {
  8. ArrayList legend = new ArrayList();
  9. public ArrayList Legend { get { return legend; } set { } }
  10.  
  11. protected PointF PointFormLarge;
  12. protected PointF PointFormSmall;
  13.  
  14. private void DrawChart(Platform g)
  15. {
  16. g.FillRectangle(g.WBrush, , , , );
  17. }
  18.  
  19. protected abstract void DrawCore(float width, Platform g, List<Composition> Compositions);
  20.  
  21. public void Draw(float width, Platform g, List<Composition> Compositions)
  22. {
  23. PointFormLarge = new PointF(width * Compositions.Count + width, );
  24. PointFormSmall = new PointF(width, );
  25. DrawChart(g);
  26. DrawCore(width, g, Compositions);
  27. }
  28. }
  29. }

Series.cs

  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System;
  4. namespace Chart
  5. {
  6. public class HistogramSeries : Series
  7. {
  8.  
  9. private void DrawAxes(Platform g)
  10. {
  11. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  12. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  13. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  14. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  15. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  16. g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
  17.  
  18. g.DrawString("月考成绩", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
  19. g.DrawString("科目", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
  20. g.DrawString("成绩", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
  21.  
  22. for (int i = ; i < ; i++)
  23. {
  24. g.DrawLine(g.BlackPen, new Point(, + * i), new Point(, + * i));
  25. }
  26. }
  27.  
  28. private void DrawLegend(Platform g)
  29. {
  30. int LegendWidth = / (Legend.Count - );
  31. int StringX = ;
  32. int LegendX = StringX + ;
  33. int k = ;
  34. foreach (string legend in Legend)
  35. {
  36. switch (k)
  37. {
  38. case :
  39. g.Brush = Brushes.Blue;
  40. break;
  41. case :
  42. g.Brush = Brushes.Red;
  43. break;
  44. case :
  45. g.Brush = Brushes.Yellow;
  46. break;
  47. case :
  48. g.Brush = Brushes.Green;
  49. break;
  50. }
  51. g.DrawString(legend, g.LargeFont, Brushes.Blue, StringX, );
  52. Rectangle rect = new Rectangle(LegendX, , LegendWidth * / , );
  53. g.FillRectangle(g.Brush, rect);
  54.  
  55. StringX += / Legend.Count;
  56. LegendX = StringX + ;
  57. k++;
  58. }
  59. }
  60.  
  61. protected override void DrawCore(float width, Platform g, List<Composition> Compositions)
  62. {
  63. DrawAxes(g);
  64. DrawLegend(g);
  65. foreach (var datapoint in Compositions[].DataPoints)
  66. {
  67. g.DrawString(datapoint.XValue, g.LargeFont, g.Bbrush, , );
  68. g.TranslateTransform(PointFormLarge.X, PointFormLarge.Y);
  69. }
  70. g.ResetTransform();
  71.  
  72. int YValueMax = ;
  73. foreach (var composition in Compositions)
  74. {
  75. if (YValueMax <= composition.Max)
  76. {
  77. YValueMax = composition.Max;
  78. }
  79. }
  80.  
  81. g.YRatioScale = / YValueMax;
  82.  
  83. for (int i = ; i <= ; i++)
  84. {
  85. g.DrawString(Math.Ceiling(// g.YRatioScale*(-i)).ToString(), g.LargeFont, g.BlackBrush, new RectangleF(, + * i, , ));
  86. }
  87.  
  88. void DrawRectangle(float x, float y, float Width, float height, Composition composition)
  89. {
  90. Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
  91. g.FillRectangle(composition.BrushColor, rect);
  92. }
  93. int j = ;
  94. foreach (var composition in Compositions)
  95. {
  96. Compositions[].BrushColor = Brushes.Blue;
  97. Compositions[].BrushColor = Brushes.Red;
  98. Compositions[].BrushColor = Brushes.Yellow;
  99. foreach (var datapoint in composition.DataPoints)
  100. {
  101. DrawRectangle(, - datapoint.YValue * g.YRatioScale, width, datapoint.YValue * g.YRatioScale, composition);
  102. g.DrawString(datapoint.YValue.ToString(), g.SmallFont, Brushes.Red, , - datapoint.YValue * g.YRatioScale - );
  103. g.TranslateTransform(PointFormLarge.X, PointFormLarge.Y);
  104. }
  105. g.ResetTransform();
  106. for (int i = ; i < j; i++)
  107. {
  108. g.TranslateTransform(PointFormSmall.X, PointFormSmall.Y);
  109. }
  110. j++;
  111. }
  112. g.ResetTransform();
  113. }
  114. }
  115. }

Histogram.cs

  1. using System.Drawing;
  2. namespace Chart
  3. {
  4. public abstract class Platform
  5. {
  6. public abstract void FillRectangle(Brush b, int x, int y, int width, int height);
  7. public abstract void DrawLine(Pen pen, Point pt1, Point pt2);
  8. public abstract void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle);
  9. public abstract void DrawString(string s, Font font, Brush brush, float x, float y);
  10. public abstract void FillRectangle(Brush brush, Rectangle rect);
  11. public abstract void TranslateTransform(float dx, float dy);
  12. public abstract void ResetTransform();
  13.  
  14. private Brush wbrush = Brushes.White;
  15. private Brush bbrush = Brushes.Blue ;
  16. private Brush blackBrush = Brushes.Black;
  17. private Brush brush ;
  18. Pen rpen = new Pen(Color.Red, );
  19. Pen blackPen = new Pen(Color .Black ,);
  20. Font largeFont = new Font("黑体", );
  21. Font smallFont = new Font("黑体", );
  22. private float yRatioScale;
  23.  
  24. public Brush WBrush { get => wbrush; set => wbrush = value; }
  25. public Pen Rpen { get => rpen; set => rpen = value; }
  26. public Font LargeFont { get => largeFont; set => largeFont = value; }
  27. public Font SmallFont { get => smallFont; set => smallFont = value; }
  28. public Brush Bbrush { get => bbrush; set => bbrush = value; }
  29. internal float YRatioScale { get => yRatioScale; set => yRatioScale = value; }
  30. public Brush Brush { get => brush; set => brush = value; }
  31. public Pen BlackPen { get => blackPen; set => blackPen = value; }
  32. public Brush BlackBrush { get => blackBrush; set => blackBrush = value; }
  33. }
  34. }

Platform.cs

  1. using System.Drawing;
  2.  
  3. namespace Chart
  4. {
  5. public class Windows : Platform
  6. {
  7. private readonly Graphics graphics;
  8.  
  9. public Windows(Bitmap bmp)
  10. {
  11. graphics = Graphics.FromImage(bmp);
  12. }
  13.  
  14. public override void FillRectangle(Brush b, int x, int y, int width, int height)
  15. {
  16. graphics.FillRectangle(b, x, y, width, height);
  17. }
  18. public override void DrawLine(Pen pen, Point pt1, Point pt2)
  19. {
  20. graphics.DrawLine(pen, pt1, pt2);
  21. }
  22. public override void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle)
  23. {
  24. graphics.DrawString(s, font, brush, layoutRectangle);
  25. }
  26. public override void DrawString(string s, Font font, Brush brush, float x, float y)
  27. {
  28. graphics.DrawString(s, font, brush, x, y);
  29. }
  30. public override void FillRectangle(Brush brush, Rectangle rect)
  31. {
  32. graphics.FillRectangle(brush, rect);
  33. }
  34. public override void TranslateTransform(float dx, float dy)
  35. {
  36. graphics.TranslateTransform(dx, dy);
  37. }
  38. public override void ResetTransform()
  39. {
  40. graphics.ResetTransform();
  41. }
  42. }
  43. }

Windows.cs

  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. namespace Chart
  4. {
  5. public class Composition
  6. {
  7. private List<DataPoint> dataPoints;
  8. private string name;
  9. private Brush brushColor;
  10. private int max;
  11.  
  12. public List<DataPoint> DataPoints { get => dataPoints; set => dataPoints = value; }
  13. public string Name { get => name; set => name = value; }
  14. public Brush BrushColor { get => brushColor; set => brushColor = value; }
  15.  
  16. public int Max //Linq中提供的计算List最大值的方法是集合中的元素即为可比较的数值类型,DataPoint不是,所以这里的方法自定义
  17. {
  18. get
  19. {
  20. foreach (var datapoint in DataPoints)
  21. {
  22. if (datapoint.YValue >= max)
  23. {
  24. max = datapoint.YValue;
  25. }
  26. }
  27. return max;
  28. }
  29. }
  30. }
  31. }

Composition.cs

  1. namespace Chart
  2. {
  3. public class DataPoint
  4. {
  5. private string xValue;
  6. private int yValue;
  7.  
  8. public int YValue { get => yValue; set => yValue = value; }
  9. public string XValue { get => xValue; set => xValue = value; }
  10. }
  11. }

DataPoint

  1. using System.Collections.Generic;
  2. using System.IO;
  3. namespace Chart
  4. {
  5. public abstract class DataSource
  6. {
  7. protected abstract List<Composition> GetDataListCore(string path);
  8.  
  9. public List<Composition> GetDataList(string path)
  10. {
  11. if (!File.Exists(path))
  12. {
  13. throw new FileNotFoundException(path);
  14. }
  15. return GetDataListCore(path);
  16. }
  17. }
  18. }

DataSource.cs

  1. using Newtonsoft.Json;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Chart
  5. {
  6. public class JsonDataSource : DataSource
  7. {
  8. protected override List<Composition> GetDataListCore(string path)
  9. {
  10. return JsonConvert.DeserializeObject<List<Composition>>(File.ReadAllText(path));
  11. }
  12. }
  13. }

JsonDataSource.cs

  1. [
  2. {
  3. "Name": "Molly",
  4. "DataPoints": [
  5. {
  6. "XValue": "English",
  7. "YValue":
  8. },
  9. {
  10. "XValue": "Chinese",
  11. "YValue":
  12. },
  13. {
  14. "XValue": "Math",
  15. "YValue":
  16. },
  17. {
  18. "XValue": "Art",
  19. "YValue":
  20. }
  21. ]
  22. },
  23. {
  24. "Name": "Bob",
  25. "DataPoints": [
  26. {
  27. "XValue": "English",
  28. "YValue":
  29. },
  30. {
  31. "XValue": "Math",
  32. "YValue":
  33. },
  34. {
  35. "XValue": "Art",
  36. "YValue":
  37. },
  38. {
  39. "XValue": "Chinese",
  40. "YValue":
  41. }
  42. ]
  43. },
  44. {
  45. "Name": "Angela",
  46. "DataPoints": [
  47. {
  48. "XValue": "English",
  49. "YValue":
  50. },
  51. {
  52. "XValue": "Math",
  53. "YValue":
  54. },
  55. {
  56. "XValue": "Art",
  57. "YValue":
  58. },
  59. {
  60. "XValue": "Chinese",
  61. "YValue":
  62. }
  63. ]
  64. }
  65.  
  66. ]

JSON.json

以下附上这个程序设计的UML类图

https://www.processon.com/view/link/5b4dbd93e4b00b08ad2085d7

C#用GDI+解析Json文件绘制Chart的更多相关文章

  1. Android--------使用gson解析json文件

    ##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...

  2. JAVA简便解析json文件

    JAVA简便解析json文件 首先放上我要解析的json文件: { "resultcode":"200", "reason":"S ...

  3. python脚本解析json文件

    python脚本解析json文件 没写完.但是有效果.初次尝试,写的比较不简洁... 比较烦的地方在于: 1,中文编码: pSpecs.decode('raw_unicode_escape') 2,花 ...

  4. 使用google-gson类库解析json文件

    使用google-gson类库解析json文件 使用JsonParser解析器来解析字符串和输入流,变成json对象 代码如下: public class Readjson { public stat ...

  5. 安卓解析JSON文件

    安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...

  6. Java解析JSON文件的方法

    http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...

  7. Logstash:解析 JSON 文件并导入到 Elasticsearch 中

    转载自:https://elasticstack.blog.csdn.net/article/details/114383426 在今天的文章中,我们将详述如何使用 Logstash 来解析 JSON ...

  8. C#解析json文件的方法

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  9. Java解析JSON文件的方法(一)

    一.首先需要在Eclipse工程中导入相关的jar包,jar包参见链接:http://yunpan.alibaba-inc.com/share/link/NdA5b6IFK 二.提供一份待解析的jso ...

随机推荐

  1. js性能优化文章集锦

    总结的js性能优化方面的小知识http://www.it165.net/pro/html/201503/35336.html 如何优化你的JS代码http://www.php100.com/html/ ...

  2. 执行.class文件

    java packageName.className即可 但是注意,如果是有包的,这段指令一定是packageName的上层目录(即bin目录)执行!

  3. es6字符串的扩展学习笔记

    1. 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. st ...

  4. debug时打到了URLClassLoader.class里面,

    一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.

  5. 将Windows下磁盘出现黑色为分配区域变成绿色区域

    在windows下不知什么原因, 有一块磁盘空间F盘就变成了黑色为分配区域. 黑色区域无法用来安装双系统, 网上查阅资料后, 找到了如何将他重新变回绿色区域的2个方法(方法二是自己无意操作成功的). ...

  6. 使用百度地图API进行坐标系转换

    最近在做移动APP的定位功能的时候发现系统GPS获取的位置信息再从百度地图API获取的实际地址总是有误差,偏离了好几个街道,但百度地图本身没这个问题.在网上查找一番发现了地图的坐标系一说,下面简单介绍 ...

  7. 运动事件Motion Events

    备注:运动事件,也是加速度时间,一般像摇晃手机就属于运动事件           监听运动事件对于UI控件有个前提就是监听对象必须是第一响应者(对于UIViewController视图控制器和UIAP ...

  8. 【Android 多媒体应用】使用 TTS

    import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speec ...

  9. C语言学习笔记--递归函数

    1. 递归函数的思想 (1)递归是一种数学上分而自治的思想,是将大型复杂问题转化为与原问题相同但规模较小的问题进行处理的一种方法 (2)递归需要有边界条件 ①当边界条件不满足时,递归继续进行 ②当边界 ...

  10. C语言学习笔记--C语言中的宏定义

    1. C 语言中的宏定义 (1)#define 是预处理器处理的单元实体之一(因此,预处理器只是简单的进行替换,并不(2)#define 定义的宏可以出现在程序的任意位置(包括函数体的内部)(3)#d ...