C#用GDI+解析Json文件绘制Chart
- using System.Collections.Generic;
- namespace Chart
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Chart chart = new Chart();
- ChartType chartType = ChartType.Histogram;
- string path = @"..\..\JSON.json";
- DataSource dataSource = new JsonDataSource();
- List<Composition> Compositions = dataSource.GetDataList(path);
- chart.Compositions.AddRange(Compositions);
- chart.Draw(chartType);
- chart.Save();
- }
- }
- }
Program.cs
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- namespace Chart
- {
- public class Chart
- {
- private Bitmap bmp = new Bitmap(, );
- List<Composition> composition = new List<Composition>();
- public List<Composition> Compositions { get { return composition; } }
- private float width;
- private float Width
- {
- get
- {
- int sum = ;
- foreach (var composition in Compositions)
- {
- sum += composition.DataPoints.Count + ;
- }
- width = (float) / sum;
- return width;
- }
- }
- public void Draw(ChartType chartType)
- {
- Series series;
- switch (chartType)
- {
- case ChartType.LineChart:
- series = new LineSeries();
- break;
- case ChartType.Histogram:
- series = new HistogramSeries();
- break;
- case ChartType.PieChart:
- series = new PieSeries();
- break;
- default:
- throw new ArgumentOutOfRangeException("Nonexistent ChartType!");
- }
- foreach (var comPosition in Compositions)
- {
- series.Legend.Add(comPosition.Name);
- }
- Platform platform = new Windows(bmp);
- series.Draw(Width, platform, Compositions);
- }
- public void Save()
- {
- bmp.Save(@"..\..\1.bmp");
- Process.Start(@"..\..\1.bmp");
- }
- }
- }
Chart.cs
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- namespace Chart
- {
- public abstract class Series
- {
- ArrayList legend = new ArrayList();
- public ArrayList Legend { get { return legend; } set { } }
- protected PointF PointFormLarge;
- protected PointF PointFormSmall;
- private void DrawChart(Platform g)
- {
- g.FillRectangle(g.WBrush, , , , );
- }
- protected abstract void DrawCore(float width, Platform g, List<Composition> Compositions);
- public void Draw(float width, Platform g, List<Composition> Compositions)
- {
- PointFormLarge = new PointF(width * Compositions.Count + width, );
- PointFormSmall = new PointF(width, );
- DrawChart(g);
- DrawCore(width, g, Compositions);
- }
- }
- }
Series.cs
- using System.Collections.Generic;
- using System.Drawing;
- using System;
- namespace Chart
- {
- public class HistogramSeries : Series
- {
- private void DrawAxes(Platform g)
- {
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawLine(g.Rpen, new Point(, ), new Point(, ));
- g.DrawString("月考成绩", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
- g.DrawString("科目", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
- g.DrawString("成绩", g.LargeFont, g.Bbrush, new RectangleF(, , , ));
- for (int i = ; i < ; i++)
- {
- g.DrawLine(g.BlackPen, new Point(, + * i), new Point(, + * i));
- }
- }
- private void DrawLegend(Platform g)
- {
- int LegendWidth = / (Legend.Count - );
- int StringX = ;
- int LegendX = StringX + ;
- int k = ;
- foreach (string legend in Legend)
- {
- switch (k)
- {
- case :
- g.Brush = Brushes.Blue;
- break;
- case :
- g.Brush = Brushes.Red;
- break;
- case :
- g.Brush = Brushes.Yellow;
- break;
- case :
- g.Brush = Brushes.Green;
- break;
- }
- g.DrawString(legend, g.LargeFont, Brushes.Blue, StringX, );
- Rectangle rect = new Rectangle(LegendX, , LegendWidth * / , );
- g.FillRectangle(g.Brush, rect);
- StringX += / Legend.Count;
- LegendX = StringX + ;
- k++;
- }
- }
- protected override void DrawCore(float width, Platform g, List<Composition> Compositions)
- {
- DrawAxes(g);
- DrawLegend(g);
- foreach (var datapoint in Compositions[].DataPoints)
- {
- g.DrawString(datapoint.XValue, g.LargeFont, g.Bbrush, , );
- g.TranslateTransform(PointFormLarge.X, PointFormLarge.Y);
- }
- g.ResetTransform();
- int YValueMax = ;
- foreach (var composition in Compositions)
- {
- if (YValueMax <= composition.Max)
- {
- YValueMax = composition.Max;
- }
- }
- g.YRatioScale = / YValueMax;
- for (int i = ; i <= ; i++)
- {
- g.DrawString(Math.Ceiling(// g.YRatioScale*(-i)).ToString(), g.LargeFont, g.BlackBrush, new RectangleF(, + * i, , ));
- }
- void DrawRectangle(float x, float y, float Width, float height, Composition composition)
- {
- Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
- g.FillRectangle(composition.BrushColor, rect);
- }
- int j = ;
- foreach (var composition in Compositions)
- {
- Compositions[].BrushColor = Brushes.Blue;
- Compositions[].BrushColor = Brushes.Red;
- Compositions[].BrushColor = Brushes.Yellow;
- foreach (var datapoint in composition.DataPoints)
- {
- DrawRectangle(, - datapoint.YValue * g.YRatioScale, width, datapoint.YValue * g.YRatioScale, composition);
- g.DrawString(datapoint.YValue.ToString(), g.SmallFont, Brushes.Red, , - datapoint.YValue * g.YRatioScale - );
- g.TranslateTransform(PointFormLarge.X, PointFormLarge.Y);
- }
- g.ResetTransform();
- for (int i = ; i < j; i++)
- {
- g.TranslateTransform(PointFormSmall.X, PointFormSmall.Y);
- }
- j++;
- }
- g.ResetTransform();
- }
- }
- }
Histogram.cs
- using System.Drawing;
- namespace Chart
- {
- public abstract class Platform
- {
- public abstract void FillRectangle(Brush b, int x, int y, int width, int height);
- public abstract void DrawLine(Pen pen, Point pt1, Point pt2);
- public abstract void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle);
- public abstract void DrawString(string s, Font font, Brush brush, float x, float y);
- public abstract void FillRectangle(Brush brush, Rectangle rect);
- public abstract void TranslateTransform(float dx, float dy);
- public abstract void ResetTransform();
- private Brush wbrush = Brushes.White;
- private Brush bbrush = Brushes.Blue ;
- private Brush blackBrush = Brushes.Black;
- private Brush brush ;
- Pen rpen = new Pen(Color.Red, );
- Pen blackPen = new Pen(Color .Black ,);
- Font largeFont = new Font("黑体", );
- Font smallFont = new Font("黑体", );
- private float yRatioScale;
- public Brush WBrush { get => wbrush; set => wbrush = value; }
- public Pen Rpen { get => rpen; set => rpen = value; }
- public Font LargeFont { get => largeFont; set => largeFont = value; }
- public Font SmallFont { get => smallFont; set => smallFont = value; }
- public Brush Bbrush { get => bbrush; set => bbrush = value; }
- internal float YRatioScale { get => yRatioScale; set => yRatioScale = value; }
- public Brush Brush { get => brush; set => brush = value; }
- public Pen BlackPen { get => blackPen; set => blackPen = value; }
- public Brush BlackBrush { get => blackBrush; set => blackBrush = value; }
- }
- }
Platform.cs
- using System.Drawing;
- namespace Chart
- {
- public class Windows : Platform
- {
- private readonly Graphics graphics;
- public Windows(Bitmap bmp)
- {
- graphics = Graphics.FromImage(bmp);
- }
- public override void FillRectangle(Brush b, int x, int y, int width, int height)
- {
- graphics.FillRectangle(b, x, y, width, height);
- }
- public override void DrawLine(Pen pen, Point pt1, Point pt2)
- {
- graphics.DrawLine(pen, pt1, pt2);
- }
- public override void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle)
- {
- graphics.DrawString(s, font, brush, layoutRectangle);
- }
- public override void DrawString(string s, Font font, Brush brush, float x, float y)
- {
- graphics.DrawString(s, font, brush, x, y);
- }
- public override void FillRectangle(Brush brush, Rectangle rect)
- {
- graphics.FillRectangle(brush, rect);
- }
- public override void TranslateTransform(float dx, float dy)
- {
- graphics.TranslateTransform(dx, dy);
- }
- public override void ResetTransform()
- {
- graphics.ResetTransform();
- }
- }
- }
Windows.cs
- using System.Collections.Generic;
- using System.Drawing;
- namespace Chart
- {
- public class Composition
- {
- private List<DataPoint> dataPoints;
- private string name;
- private Brush brushColor;
- private int max;
- public List<DataPoint> DataPoints { get => dataPoints; set => dataPoints = value; }
- public string Name { get => name; set => name = value; }
- public Brush BrushColor { get => brushColor; set => brushColor = value; }
- public int Max //Linq中提供的计算List最大值的方法是集合中的元素即为可比较的数值类型,DataPoint不是,所以这里的方法自定义
- {
- get
- {
- foreach (var datapoint in DataPoints)
- {
- if (datapoint.YValue >= max)
- {
- max = datapoint.YValue;
- }
- }
- return max;
- }
- }
- }
- }
Composition.cs
- namespace Chart
- {
- public class DataPoint
- {
- private string xValue;
- private int yValue;
- public int YValue { get => yValue; set => yValue = value; }
- public string XValue { get => xValue; set => xValue = value; }
- }
- }
DataPoint
- using System.Collections.Generic;
- using System.IO;
- namespace Chart
- {
- public abstract class DataSource
- {
- protected abstract List<Composition> GetDataListCore(string path);
- public List<Composition> GetDataList(string path)
- {
- if (!File.Exists(path))
- {
- throw new FileNotFoundException(path);
- }
- return GetDataListCore(path);
- }
- }
- }
DataSource.cs
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using System.IO;
- namespace Chart
- {
- public class JsonDataSource : DataSource
- {
- protected override List<Composition> GetDataListCore(string path)
- {
- return JsonConvert.DeserializeObject<List<Composition>>(File.ReadAllText(path));
- }
- }
- }
JsonDataSource.cs
- [
- {
- "Name": "Molly",
- "DataPoints": [
- {
- "XValue": "English",
- "YValue":
- },
- {
- "XValue": "Chinese",
- "YValue":
- },
- {
- "XValue": "Math",
- "YValue":
- },
- {
- "XValue": "Art",
- "YValue":
- }
- ]
- },
- {
- "Name": "Bob",
- "DataPoints": [
- {
- "XValue": "English",
- "YValue":
- },
- {
- "XValue": "Math",
- "YValue":
- },
- {
- "XValue": "Art",
- "YValue":
- },
- {
- "XValue": "Chinese",
- "YValue":
- }
- ]
- },
- {
- "Name": "Angela",
- "DataPoints": [
- {
- "XValue": "English",
- "YValue":
- },
- {
- "XValue": "Math",
- "YValue":
- },
- {
- "XValue": "Art",
- "YValue":
- },
- {
- "XValue": "Chinese",
- "YValue":
- }
- ]
- }
- ]
JSON.json
以下附上这个程序设计的UML类图
https://www.processon.com/view/link/5b4dbd93e4b00b08ad2085d7
C#用GDI+解析Json文件绘制Chart的更多相关文章
- Android--------使用gson解析json文件
##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...
- JAVA简便解析json文件
JAVA简便解析json文件 首先放上我要解析的json文件: { "resultcode":"200", "reason":"S ...
- python脚本解析json文件
python脚本解析json文件 没写完.但是有效果.初次尝试,写的比较不简洁... 比较烦的地方在于: 1,中文编码: pSpecs.decode('raw_unicode_escape') 2,花 ...
- 使用google-gson类库解析json文件
使用google-gson类库解析json文件 使用JsonParser解析器来解析字符串和输入流,变成json对象 代码如下: public class Readjson { public stat ...
- 安卓解析JSON文件
安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...
- Java解析JSON文件的方法
http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...
- Logstash:解析 JSON 文件并导入到 Elasticsearch 中
转载自:https://elasticstack.blog.csdn.net/article/details/114383426 在今天的文章中,我们将详述如何使用 Logstash 来解析 JSON ...
- C#解析json文件的方法
C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...
- Java解析JSON文件的方法(一)
一.首先需要在Eclipse工程中导入相关的jar包,jar包参见链接:http://yunpan.alibaba-inc.com/share/link/NdA5b6IFK 二.提供一份待解析的jso ...
随机推荐
- js性能优化文章集锦
总结的js性能优化方面的小知识http://www.it165.net/pro/html/201503/35336.html 如何优化你的JS代码http://www.php100.com/html/ ...
- 执行.class文件
java packageName.className即可 但是注意,如果是有包的,这段指令一定是packageName的上层目录(即bin目录)执行!
- es6字符串的扩展学习笔记
1. 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. st ...
- debug时打到了URLClassLoader.class里面,
一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.
- 将Windows下磁盘出现黑色为分配区域变成绿色区域
在windows下不知什么原因, 有一块磁盘空间F盘就变成了黑色为分配区域. 黑色区域无法用来安装双系统, 网上查阅资料后, 找到了如何将他重新变回绿色区域的2个方法(方法二是自己无意操作成功的). ...
- 使用百度地图API进行坐标系转换
最近在做移动APP的定位功能的时候发现系统GPS获取的位置信息再从百度地图API获取的实际地址总是有误差,偏离了好几个街道,但百度地图本身没这个问题.在网上查找一番发现了地图的坐标系一说,下面简单介绍 ...
- 运动事件Motion Events
备注:运动事件,也是加速度时间,一般像摇晃手机就属于运动事件 监听运动事件对于UI控件有个前提就是监听对象必须是第一响应者(对于UIViewController视图控制器和UIAP ...
- 【Android 多媒体应用】使用 TTS
import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speec ...
- C语言学习笔记--递归函数
1. 递归函数的思想 (1)递归是一种数学上分而自治的思想,是将大型复杂问题转化为与原问题相同但规模较小的问题进行处理的一种方法 (2)递归需要有边界条件 ①当边界条件不满足时,递归继续进行 ②当边界 ...
- C语言学习笔记--C语言中的宏定义
1. C 语言中的宏定义 (1)#define 是预处理器处理的单元实体之一(因此,预处理器只是简单的进行替换,并不(2)#define 定义的宏可以出现在程序的任意位置(包括函数体的内部)(3)#d ...