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 ...
随机推荐
- Kosaraju
https://www.cnblogs.com/nullzx/p/6437926.html
- StringBuilder、StringBuffer、String区别
相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,今天重新把这个概念给大家复习一下,顺便牵出 J2 ...
- Android高仿京东淘宝自动无限循环轮播控件的实现思路和过程
在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的实现思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于 ...
- SqlServer 用户和权限操作
use [master] GO --创建用户,Test,密码Test CREATE LOGIN [TestUser] WITH PASSWORD=N'Test', DEFAULT_DATABASE=[ ...
- Quartz 定时任务(含Redis)
一 定时任务基础: MQ:Message Queue消息队列服务器:MSMQ.ActiveMQ.Redis等项目任务:确定邮件的发送,重置密码的发送(发送可能会很慢,而且有可能还需要重试),用消息队 ...
- 蓝桥杯 算法训练 ALGO-149 5-2求指数
算法训练 5-2求指数 时间限制:1.0s 内存限制:256.0MB 问题描述 已知n和m,打印n^1,n^2,...,n^m.要求用静态变量实现.n^m表示n的m次方.已知n和m,打印n^ ...
- 使用Spring Boot 和Spring Data JPA访问mysql数据库
在Spring中使用JdbcTemplate是一种基本的数据访问方式,但是仍然需要较多的代码,为了解决这些大量枯燥的数据操作语句,我们可以使用ORM框架,比如:Hibernate,通过整合Hibern ...
- ORACLE增加用户
create user 账号 identified by "密码"; grant connect to 账号; grant resource to 账号; --把dba 权限给in ...
- SA读书笔记1
SA的基本任务: 帐户: 为新用户增设帐号,将不再活动的帐号删除,帐号存活期事务(忘记密码等).把用户的主目录放在什么位置.在哪些机器上创建帐号. 硬件:识别并使用新硬件.对于虚拟化:设备可能要安装在 ...
- mybatis 学习三 关键文件解析
1: mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...