mschart 实现统计图表绘制

<一>

最近要实现查询统计功能,综合比较了MSChart,OWC11和GDI+等实现工具,觉得MSChart能比较方便地实现所需功能。

在做该部分的时候,开始觉得没思路,很困难。后来请教了LYJ,他提出两点,一是确定用什么工具进行查询,二是确定要展现给用户的是什么,也就是要统计的内容。确定这两点,就可以写查询语句了。

因为自己水平也比较菜,所以到今天大致实现还是花了挺长的时间的。一部分是考虑统计指标,之前想的可能太复杂,想要统计的很多。后来还是省事直接用WHB写的查询controller,当然大框架搭好后还是得修改下。

还有一部分时间是用来熟悉MSCHART,看了微软提供的例子,看网上找的文档和博客,看帮助文档。发现自己的一个问题是,静不下心来先看别人的例子,好像不习惯先打基础。总要自己写不出来的时候,才去搜索,才能认真看别人是如何实现的。应该好好改进这个问题。

从一定意义上说,这是我自己写程序的开端。因为之前做的几个功能,没有真正去深入地写和思考,大部分都是在他人实现的基础上改写的。好的开始是成功的一半,加油!

<二>

程序中主要实现的代码如下

代码
#region 画柱状图
public void DrawBar(int[] All, int[] Done)
{
IStatisticHelper helper
= new StatisticHelper();
List
<Compare> CList = new List<Compare>();
Compare _com
= new Compare();
Detail _detail
= new Detail();
_detail._value_remark
= "记录总数";
_detail._compare_value_remark
= "记录处理数";
UserstatisticFilter filter
= Filter;
//记录处理数统计
Series series = new Series("series");
series.ChartType
= SeriesChartType.Column;
for (int pointIndex = 0; pointIndex < All.Length; pointIndex++)
{
_com._value
= All[pointIndex];
_com._compare_value
= Done[pointIndex];
series.Points.AddY(Done[pointIndex]);
}
series.LegendText
= _detail._compare_value_remark;

Chart1.Series.Add(series);
Chart1.ChartAreas[
"ChartArea1"].AxisX.ArrowStyle = AxisArrowStyle.Triangle;
Chart1.ChartAreas[
0].AxisY.ArrowStyle = AxisArrowStyle.Triangle;
Chart1.ChartAreas[
0].AxisX.Title = "时间";
Chart1.ChartAreas[
0].AxisY.Title = "记录数";
Chart1.ChartAreas[
0].AxisX.MajorTickMark.Enabled = false;
Chart1.ChartAreas[
0].AxisY.MajorTickMark.Enabled = false;
Chart1.Legends[
0].Font = new Font("宋体", 10);
Chart1.ChartAreas[
0].AxisX.TitleFont = new Font("宋体", 10f);
Chart1.ChartAreas[
0].AxisY.TitleFont = new Font("宋体", 10f);
Chart1.ChartAreas[
0].AxisX.Interval = 1;
Chart1.ChartAreas[
0].AxisX.IsLabelAutoFit = true;
Chart1.ChartAreas[
0].AxisX.LabelStyle.Format = "#月 ";
Series series1
= new Series("series1");
series1.ChartType
= SeriesChartType.Column;
for (int pointIndex = 0; pointIndex < All.Length; pointIndex++)
{
series1.Points.AddY(All[pointIndex]);
}
series1.LegendText
= _detail._value_remark;
Chart1.Series.Add(series1);
}

#endregion

运行后的结果图

 

<三>

参考的一些属性设置,来自http://www.cnblogs.com/ashou706/archive/2010/06/01/1749257.html,感谢作者

  /// <summary>
    /// 设置mschart样式
    /// </summary>
    private void SetMSChartStyle()
    {
        //绘图前期处理
        chartCWPBestMode.Titles.Clear();

        //标题设置
        Title title = new Title();
        title.Text = "循环水泵最佳运行方式";
        title.Font = new Font("宋体", 16f, FontStyle.Bold);
        //标题
        chartCWPBestMode.Titles.Add(title);

        // 坐标轴设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.IsMarginVisible = false;

        //X 轴坐标最大最小值
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Minimum = 5;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Maximum = 35;

        // 坐标轴刻度线不延长出来设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorTickMark.Enabled = false;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorTickMark.Enabled = false;

        //X 次要辅助线设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.Enabled = true;
        //X 次要辅助线间距
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.Interval = 1;
        //X 次要辅助线颜色
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.LineColor = Color.LightGray;

        //Y 次要辅助线设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.Enabled = true;
        //Y 次要辅助线间距
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.Interval = 10;
        //Y 次要辅助线颜色
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.LineColor = Color.LightGray;

        //X 主要辅助线设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.Enabled = true;
        //X 主要辅助线间距
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.Interval = 5;
        //X 主要辅助线颜色
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.LineColor = Color.Black;

        //Y 主要辅助线设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.Enabled = true;
        //Y 主要辅助线间距
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.Interval = 30;
        //Y 主要辅助线颜色
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.LineColor = Color.Black;

        //坐标主要辅助线刻度间距
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Interval = 5;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.Interval = 30;

        //坐标轴说明
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Title = "凝汽器冷却水进口温度(℃)";
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.Title = "机组负荷(MW)";
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.TitleFont = new Font("宋体", 10f, FontStyle.Bold);
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.TitleFont = new Font("宋体", 10f, FontStyle.Bold);
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.TitleAlignment = StringAlignment.Far;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.TitleAlignment = StringAlignment.Far;

        //边框样式设置
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderColor = Color.Black;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderDashStyle = ChartDashStyle.Solid;
        chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderWidth = 2;

        //图例文字
        chartCWPBestMode.Series["SeriesCurrentMode"].LegendText = "当前运行方式";
        chartCWPBestMode.Series["SeriesTRAN1"].LegendText = "单泵高速切换曲线";
        chartCWPBestMode.Series["SeriesTRAN2"].LegendText = "两机三泵切换曲线";
        chartCWPBestMode.Series["SeriesTRAN3"].LegendText = "一高一低切换曲线";
        chartCWPBestMode.Series["SeriesTRAN4"].LegendText = "两泵高速切换曲线";

        //图例位置、字体设置;坐标轴位置设定
        chartCWPBestMode.Legends[0].Position = new ElementPosition(10, 10, 88, 7);
        chartCWPBestMode.Legends[0].Font = new Font("宋体", 9);
        chartCWPBestMode.ChartAreas[0].InnerPlotPosition = new ElementPosition(6, 5, 90, 82);
    }

原文地址:https://www.cnblogs.com/gisalameda/p/1857748.html