C#在winform平台构建堆叠柱状图

2019-12-30  本文已影响0人  GoodTekken

前言

之前在网上找了很多关于状态图的资料,但是很少是在winform上实现的。其实堆叠柱状图是来自于PPT的一种数据统计方式,展示给用户直观的图片印象。而C#很多时候要展现比较精美的界面都采用WPF来实现,像winform的技术一直没有更新,控件中只有老旧的chart控件,很难实现我们的需求,因此在winform平台上要实现堆叠柱状图需要引入第三方插件,调用的是百度开源的echart
echart官网:
https://www.echartsjs.com/examples/zh/index.html

准备

同时,为了可以添加HTML文件和修改HTML,增加HTML.cs文件,源代码如下:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    namespace MyEchart
    {
        public struct HTML
        {
            public static void EditHtml(string fileName)
            {
                Stream myStream = new FileStream(fileName, FileMode.Open);
                //Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
                Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
                StreamReader myStreamReader = new StreamReader(myStream, encode);
                string strhtml = myStreamReader.ReadToEnd();
                //string stroutput = strhtml.Replace("&*平均温度*&", "要替换的内容平均温度");
                string stroutput = strhtml.Replace("123", "123456");
                myStream.Seek(0, SeekOrigin.Begin);
                myStream.SetLength(0);


                StreamWriter sw = new StreamWriter(myStream, encode);
                sw.Write(stroutput);
                sw.Flush();
                sw.Close();
                myStream.Close();
            }

            public static void AddHtml(string path)
            {
                try
                {
                    string fileName = path + ".html";
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    using (StreamWriter sw = new StreamWriter(fileName, false))
                    {
                        //sw.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
                        //sw.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
                        //sw.WriteLine("<head>");
                        //sw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
                        //sw.WriteLine("<title> </title>");
                        //sw.WriteLine("</head>");
                        //sw.WriteLine("<body>");
                        //sw.WriteLine("<img src=" + path + ">");
                        //sw.WriteLine("</body>");
                        //sw.WriteLine("</html>");

                        sw.WriteLine("<!DOCTYPE html>");
                        sw.WriteLine("<html>");
                        sw.WriteLine("<head>");
                        sw.WriteLine("    <meta charset=\"utf-8\">");
                        sw.WriteLine("    <title>ECharts</title>");
                        sw.WriteLine("    <!-- 引入 echarts.js -->");
                        sw.WriteLine("    <script src=\"echarts.js\"></script>");
                        sw.WriteLine("</head>");
                        sw.WriteLine("<body>");
                        sw.WriteLine("    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->");
                        sw.WriteLine("    <div id=\"main\" style=\"width: 1132px;height:516px;\"></div>");
                        sw.WriteLine("    <script type=\"text/javascript\">");
                        sw.WriteLine("        // 基于准备好的dom,初始化echarts实例");
                        sw.WriteLine("        var myChart = echarts.init(document.getElementById('main'));");
                        sw.WriteLine("");
                        sw.WriteLine("");
                        sw.WriteLine("        option = {");
                        sw.WriteLine("    tooltip: {");
                        sw.WriteLine("        trigger: 'axis',");
                        sw.WriteLine("        axisPointer: {");
                        sw.WriteLine("            type: 'cross',");
                        sw.WriteLine("            crossStyle: {");
                        sw.WriteLine("                color: '#999'");
                        sw.WriteLine("            }");
                        sw.WriteLine("        }");
                        sw.WriteLine("    },");
                        sw.WriteLine("    toolbox: {");
                        sw.WriteLine("        feature: {");
                        sw.WriteLine("            dataView: {show: true, readOnly: false},");
                        sw.WriteLine("            magicType: {show: true, type: ['line', 'bar']},");
                        sw.WriteLine("            restore: {show: true},");
                        sw.WriteLine("            saveAsImage: {show: true}");
                        sw.WriteLine("        }");
                        sw.WriteLine("    },");
                        sw.WriteLine("    legend: {");
                        sw.WriteLine("        data:['蒸发量','降水量', '1']");
                        sw.WriteLine("    },");
                        sw.WriteLine("    xAxis: [");
                        sw.WriteLine("        {");
                        sw.WriteLine("            type: 'category',");
                        sw.WriteLine("            data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],");
                        sw.WriteLine("            axisPointer: {");
                        sw.WriteLine("                type: 'shadow'");
                        sw.WriteLine("            }");
                        sw.WriteLine("        }");
                        sw.WriteLine("    ],");
                        sw.WriteLine("    yAxis: [");
                        sw.WriteLine("        {");
                        sw.WriteLine("            type: 'value',");
                        sw.WriteLine("            name: '水量',");
                        sw.WriteLine("            min: 0,");
                        sw.WriteLine("            max: 250,");
                        sw.WriteLine("            interval: 50,");
                        sw.WriteLine("            axisLabel: {");
                        sw.WriteLine("                formatter: '{value} ml'");
                        sw.WriteLine("            }");
                        sw.WriteLine("        },");
                        sw.WriteLine("        {");
                        sw.WriteLine("            type: 'value',");
                        sw.WriteLine("            name: '温度',");
                        sw.WriteLine("            min: 0,");
                        sw.WriteLine("            max: 25,");
                        sw.WriteLine("            interval: 5,");
                        sw.WriteLine("            axisLabel: {");
                        sw.WriteLine("                formatter: '{value} °C'");
                        sw.WriteLine("            }");
                        sw.WriteLine("        }");
                        sw.WriteLine("    ],");
                        sw.WriteLine("    series: [");
                        sw.WriteLine("        {");
                        sw.WriteLine("            name:'蒸发量',");
                        sw.WriteLine("            type:'bar',");
                        sw.WriteLine("            stack:'堆叠',//堆叠柱状图在此设置成统一名称即可");
                        sw.WriteLine("            data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]");
                        sw.WriteLine("        },");
                        sw.WriteLine("        {");
                        sw.WriteLine("            name:'降水量',");
                        sw.WriteLine("            type:'bar',");
                        sw.WriteLine("            stack:'堆叠',//堆叠柱状图在此设置成统一名称即可");
                        sw.WriteLine("            data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]");
                        sw.WriteLine("        },");
                        sw.WriteLine("        {");
                        sw.WriteLine("            name:'1',");
                        sw.WriteLine("            type:'line',");
                        sw.WriteLine("            yAxisIndex: 1,");
                        sw.WriteLine("            data:[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]");
                        sw.WriteLine("        }");
                        sw.WriteLine("    ]");
                        sw.WriteLine("};");
                        sw.WriteLine("");
                        sw.WriteLine("        // 使用刚指定的配置项和数据显示图表。");
                        sw.WriteLine("        myChart.setOption(option);");
                        sw.WriteLine("    </script>");
                        sw.WriteLine("</body>");
                        sw.WriteLine("</html>");
                        sw.Close();
                    }
                }
                catch(ArithmeticException ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
    }

以上是构建的所有源代码,欢迎交流。

///////////////////////我是分割线*///////////////////////////
(2019年12月30日补充)

当然啦,我们看到的官网HTML文件是开源的,我们要想拿过来直接用,就要自己制作HTML文件了。

散点图.png
将左侧的源代码放进debug文件夹的准备生成的文件.txt中,使用以下代码转换便可得到HTML文件了。
        public static void MakOptionFile(string path)
        {
            //获取应用程序的当前工作目录
            string filepath = System.IO.Directory.GetCurrentDirectory();
            try
            {
                string fileName = path + ".txt";
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                using (StreamWriter sw = new StreamWriter(fileName, false))
                {
                    string[] strs = File.ReadAllLines(filepath+ @"\准备生成的文件.txt", System.Text.Encoding.GetEncoding("gb2312"));

                    foreach(string strTemp in strs)
                    {
                        sw.WriteLine("sw.WriteLine(\""+ strTemp + "\");");
                    }
                    sw.Close();
                }
            }
            catch (ArithmeticException ex)
            {
                Console.WriteLine(ex);
            }
        }

通过以上步骤,便可在winform平台创建堆叠柱状图了,按照惯例,在文章末尾贴上一张效果图吧。^ - ^


效果图.png
上一篇 下一篇

猜你喜欢

热点阅读