欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#用GDI+解析Json文件绘制Chart

程序员文章站 2022-05-13 23:45:48
using System.Collections.Generic; namespace Chart { public class Program { static void Main(string[] args) { Chart chart = new Chart(); ChartType char ......

C#用GDI+解析Json文件绘制Chart

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

 

C#用GDI+解析Json文件绘制Chart
using system;
using system.collections.generic;
using system.diagnostics;
using system.drawing;
namespace chart
{
    public class chart
    {
        private bitmap bmp = new bitmap(600, 600);
        list<composition> composition = new list<composition>();
        public list<composition> compositions { get { return composition; } }
        private float width;

        private float width
        {
            get
            {
                int sum = 0;
                foreach (var composition in compositions)
                {
                    sum += composition.datapoints.count + 1;
                }
                width = (float)420 / 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
C#用GDI+解析Json文件绘制Chart
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, 20, 20, 580, 500);
        }

        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, 0);
            pointformsmall = new pointf(width, 0);
            drawchart(g);
            drawcore(width, g, compositions);
        }
    }
}
series.cs
C#用GDI+解析Json文件绘制Chart
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(100, 40), new point(100, 420));
            g.drawline(g.rpen, new point(100, 40), new point(90, 50));
            g.drawline(g.rpen, new point(100, 40), new point(110, 50));
            g.drawline(g.rpen, new point(100, 420), new point(570, 420));
            g.drawline(g.rpen, new point(570, 420), new point(560, 410));
            g.drawline(g.rpen, new point(570, 420), new point(560, 430));

            g.drawstring("月考成绩", g.largefont, g.bbrush, new rectanglef(300, 30, 170, 50));
            g.drawstring("科目", g.largefont, g.bbrush, new rectanglef(530, 450, 100, 40));
            g.drawstring("成绩", g.largefont, g.bbrush, new rectanglef(40, 30, 40, 40));

            for (int i = 0; i < 5; i++)
            {
                g.drawline(g.blackpen, new point(100, 60 + 72 * i), new point(570, 60 + 72 * i));
            }
        }

        private void drawlegend(platform g)
        {
            int legendwidth = 250 / (legend.count - 1);
            int stringx = 50;
            int legendx = stringx + 60;
            int k = 0;
            foreach (string legend in legend)
            {
                switch (k)
                {
                    case 0:
                        g.brush = brushes.blue;
                        break;
                    case 1:
                        g.brush = brushes.red;
                        break;
                    case 2:
                        g.brush = brushes.yellow;
                        break;
                    case 3:
                        g.brush = brushes.green;
                        break;
                }
                g.drawstring(legend, g.largefont, brushes.blue, stringx, 480);
                rectangle rect = new rectangle(legendx, 480, legendwidth * 2 / 3, 20);
                g.fillrectangle(g.brush, rect);

                stringx += 550 / legend.count;
                legendx = stringx + 60;
                k++;
            }
        }



        protected override void drawcore(float width, platform g, list<composition> compositions)
        {
            drawaxes(g);
            drawlegend(g);
            foreach (var datapoint in compositions[0].datapoints)
            {
                g.drawstring(datapoint.xvalue, g.largefont, g.bbrush, 120, 430);
                g.translatetransform(pointformlarge.x, pointformlarge.y);
            }
            g.resettransform();

            int yvaluemax = 0;
            foreach (var composition in compositions)
            {
                if (yvaluemax <= composition.max)
                {
                    yvaluemax = composition.max;
                }
            }

            g.yratioscale = 370 / yvaluemax;


            for (int i = 0; i <= 5; i++)
            {
                g.drawstring(math.ceiling(360/5/ g.yratioscale*(5-i)).tostring(), g.largefont, g.blackbrush, new rectanglef(80, 50 + 72 * i, 50, 50));
            }




            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 = 1;
            foreach (var composition in compositions)
            {
                compositions[0].brushcolor = brushes.blue;
                compositions[1].brushcolor = brushes.red;
                compositions[2].brushcolor = brushes.yellow;
                foreach (var datapoint in composition.datapoints)
                {
                    drawrectangle(120, 420 - datapoint.yvalue * g.yratioscale, width, datapoint.yvalue * g.yratioscale, composition);
                    g.drawstring(datapoint.yvalue.tostring(), g.smallfont, brushes.red, 120, 420 - datapoint.yvalue * g.yratioscale - 15);
                    g.translatetransform(pointformlarge.x, pointformlarge.y);
                }
                g.resettransform();
                for (int i = 0; i < j; i++)
                {
                    g.translatetransform(pointformsmall.x, pointformsmall.y);
                }
                j++;
            }
            g.resettransform();
        }
    }
}
histogram.cs
C#用GDI+解析Json文件绘制Chart
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, 3);
        pen blackpen = new pen(color .black ,1);
        font largefont = new font("黑体", 12);
        font smallfont = new font("黑体", 8);
        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
C#用GDI+解析Json文件绘制Chart
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
C#用GDI+解析Json文件绘制Chart
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
C#用GDI+解析Json文件绘制Chart
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
C#用GDI+解析Json文件绘制Chart
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
C#用GDI+解析Json文件绘制Chart
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
C#用GDI+解析Json文件绘制Chart
[
  {
    "name": "molly",
    "datapoints": [
      {
        "xvalue": "english",
        "yvalue": 2
      },
      {
        "xvalue": "chinese",
        "yvalue": 6
      },
      {
        "xvalue": "math",
        "yvalue": 7
      },
      {
        "xvalue": "art",
        "yvalue": 7
      }
    ]
  },
  {
    "name": "bob",
    "datapoints": [
      {
        "xvalue": "english",
        "yvalue": 9
      },
      {
        "xvalue": "math",
        "yvalue": 12
      },
      {
        "xvalue": "art",
        "yvalue": 3
      },
      {
        "xvalue": "chinese",
        "yvalue": 3
      }
    ]
  },
  {
    "name": "angela",
    "datapoints": [
      {
        "xvalue": "english",
        "yvalue": 5
      },
      {
        "xvalue": "math",
        "yvalue": 13
      },
      {
        "xvalue": "art",
        "yvalue": 9
      },
      {
        "xvalue": "chinese",
        "yvalue": 6
      }
    ]
  }

]
json.json

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

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

C#用GDI+解析Json文件绘制Chart