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

2019-12-11 08:06栏目:bob体育平台
TAG:

图片 1

复制代码 代码如下:import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class TestPage {public TestPage() {}public static void main {String driver = "oracle.jdbc.driver.OracleDriver";String strUrl = "jdbc:oracle:thin:@localhost:1521:xmgl";String username = "xmgl";String password = "xmgl";Statement stmt = null;ResultSet rs = null;Connection conn = null;try {Class.forName;conn = DriverManager.getConnection(strUrl, username, password);System.out.println;CallableStatement cs = null;//创建CallableStatementcs = conn.prepareCall("{call page.fenye;cs.setString;//表名cs.setInt;//显示几条记录cs.setInt;//第几页//注册总记录数,声明输出参数是什么类型的 cs.registerOutParameter(4,oracle.jdbc.OracleTypes.INTEGER);//注册总页数cs.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER);//注册返回的结果集cs.registerOutParameter(6,oracle.jdbc.OracleTypes.CURSOR);cs.execute();//取出总记录数 /这里要注意,getInt中4,是由该参数的位置决定的int rowNum=cs.getInt;//获得输出参数int pageCount = cs.getIntcs.getObject;System.out.println;System.out.println;while {System.out.println+" 名字:"+rs.getString+" 密码:"+rs.getString;}} catch {ex2.printStackTrace();} catch {ex2.printStackTrace();} finally {try {if {rs.close {stmt.close {conn.close();}}} catch {}}}}

说到, 尽量使用mysqli_set_charset而不是”SET NAMES”, 当然, 这个内容在PHP手册中也有叙及, 但是却没有解释为什么. 最近有好几个朋友问我这个问题, 到底为什么? 问的人多了, 我也就觉得可以写篇blog, 专门介绍下这部分的内容了. 首先, 很多人都不知道”SET NAMES”到底是做了什么, 我之前的文章深入MySQL字符集设置中, 曾经介绍过character_set_client/character_set_connection/character_set_results这三个MySQL的”环境变量”, 这里再简单介绍下, 这三个变量, 分别告诉MySQL服务器, 客户端的编码集, 在传输给MySQL服务器的时候的编码集, 以及期望MySQL返回的结果的编码集. 比如, 通过使用”SET NAMES utf8″, 就告诉服务器, 我用的是utf-8编码, 我希望你也给我返回utf-8编码的查询结果. 一般情况下, 使用”SET NAMES”就足够了, 也是可以保证正确的. 那么为什么手册又要说推荐使用mysqli_set_charset呢? 首先, 我们看看mysqli_set_charset到底做了什么(注意星号注释处, mysql_set_charset类似): 复制代码 代码如下: //php-5.2.11-SRC/ext/mysqli/mysqli_nonapi.c line 342 PHP_FUNCTION { MY_MYSQL *mysql; zval *mysql_link; char *cs_name = NULL; unsigned int len; if (zend_parse_method_parameters TSRMLS_CC, getThis() , "Os", &mysql_link, mysqli_link_class_entry, &cs_name, &len) == FAILURE) { return; } MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &mysql_link, "mysqli_link" , MYSQLI_STATUS_VALID); if (mysql_set_character_set(mysql->mysql, cs_name)) { //** 调用libmysql的对应函数 RETURN_FALSE; } RETURN_TRUE; } 那mysql_set_character_set又做了什么呢? 复制代码 代码如下: //mysql-5.1.30-SRC/libmysql/client.c, line 3166: int STDCALL mysql_set_character_set(MYSQL *mysql, const char *cs_name) { struct charset_info_st *cs; const char *save_csdir= charsets_dir; if (mysql->options.charset_dir) charsets_dir= mysql->options.charset_dir; if < MY_CS_NAME_SIZE && (cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF { char buff[MY_CS_NAME_SIZE + 10]; charsets_dir= save_csdir; /* Skip execution of "SET NAMES" for pre-4.1 servers */ if (mysql_get_server_version return 0; sprintf(buff, "SET NAMES %s", cs_name); if (!mysql_real_query(mysql, buff, strlen { mysql->charset= cs; } } //以下省略 我们可以看到, mysqli_set_charset除了做了”SET NAMES”以外, 还多做了一步: 复制代码 代码如下: sprintf(buff, "SET NAMES %s", cs_name); if (!mysql_real_query(mysql, buff, strlen { mysql->charset= cs; } 而对于mysql这个核心结构的成员charset又有什么作用呢? 这就要说说mysql_real_escape_string()了, 这个函数和mysql_escape_string的区别就是, 它会考虑”当前”字符集. 那么这个当前字符集从哪里来呢? 对了, 你猜的没错, 就是mysql->charset. mysql_real_string在判断宽字符集的字符的时候, 就根据这个成员变量来分别采用不同的策略, 比如如果是utf-8, 那么就会采用libmysql/ctype-utf8.c. 看个实例, 默认mysql连接字符集是latin-1, : 复制代码 代码如下: 因为, “慭”的gbk编码低字节为5c, 也就是ascii中的””, 而因为除了mysql_set_charset影响mysql->charset以外, 其他时刻mysql->charset都为默认值, 所以, 结果就是: 复制代码 代码如下: $ php -f 5c.php string "慭" string "慭" 大家现在很清楚了吧?

图片 2图片 3

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

 

图片 4图片 5

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

图片 6图片 7

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

图片 8图片 9

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();
        }
    }
}

版权声明:本文由bob体育app发布于bob体育平台,转载请注明出处:C#用GDI+解析Json文件绘制Chart