zoukankan      html  css  js  c++  java
  • Owc 绘制曲线

         前些日子,由于公司需要web曲线的功能,因此这部分工作让我去实验、研究。本人也是第一次弄这个东西,通过在网上查资料,自己看帮助,总算弄出了曲线图形。
         效果图如下:
     
    Default.cs.aspx:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chartY();
            }
        }

        private DataSet dataset()
        {
            //连接数据库并获取特定字符串
            //string strSeriesName = "图形";
            string ConnectString = "Server=(local);DataBase=Owc;Uid=sa;Pwd=";
            string Sql = "select month,Allcount,fee from chart";
            SqlConnection myConn = new SqlConnection(ConnectString);
            myConn.Open();
            SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
            DataSet ds = new DataSet();
            Da.Fill(ds);
            return ds;
        }

    #region 绘制两个Y轴
        /// <summary>
        /// 绘制两个Y轴
        /// </summary>
        private void chartY()
        {
            DataSet ds = dataset();

            //显示图表名称
            string strSeriesName = "图形";
            //存放月
            string[] MonNum = new string[12];
            //存放数据
            string[] MonCount = new string[12];
            //存放钱数
            string[] MonFee = new string[12];
            //为数组赋值
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                MonNum[i] = ds.Tables[0].Rows[i][0].ToString();
                MonCount[i] = ds.Tables[0].Rows[i][1].ToString();
                MonFee[i] = ds.Tables[0].Rows[i][2].ToString();
            }

            //为x轴指定特定字符串,以便显示数据
            string strXdata = String.Empty;
            foreach (string strData in MonNum)
            {
                strXdata += strData + "\t";
            }

            string strYdata = String.Empty;
            //为y轴指定特定的字符串,以便与x轴相对应
            foreach (string strValue in MonCount)
            {
                strYdata += strValue + "\t";
            }

            string strYdata1 = String.Empty;
            foreach (string strValue1 in MonFee)
            {
                strYdata1 += strValue1 + "\t";
            }

            //创建ChartSpace对象来放置图表
            ChartSpace laySpace = new ChartSpaceClass();

            //在ChartSpace对象中添加图表
            ChChart InsertChart = laySpace.Charts.Add(0);

            //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
            InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
            //InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
            //InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
            //InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
            //InsertChart.Type = ChartChartTypeEnum.chChartTypeSmoothLine;  //弧线图
            //InsertChart.Type = ChartChartTypeEnum.chChartTypePie;    // 饼图

     


            //指定图表是否需要图例标注
            InsertChart.HasLegend = true;


            InsertChart.HasTitle = true;//为图表添加标题
            InsertChart.Title.Caption = "2007年本人每个月花销流水账";//标题名称

            //为x,y轴添加图示说明
            InsertChart.Axes[0].HasTitle = true;
            InsertChart.Axes[0].Title.Caption = "月份(月)";//月份
            InsertChart.Axes[1].HasTitle = true;
            //InsertChart.Axes[1].Scaling.SplitMinimum = 200;
            InsertChart.Axes[1].Title.Caption = "数量(元)";
            //InsertChart.Axes[1].

            //显示Y轴刻度标签
            InsertChart.Axes[1].HasTickLabels  = true ;

            //不显示Y轴主要刻度标记
            //InsertChart.Axes[1].MajorTickMarks = ChartTickMarkEnum.chTickMarkNone;

            //Y轴主要网络线
            InsertChart.Axes[1].HasMajorGridlines = false;
            InsertChart.Axes[1].HasAutoMajorUnit = true;

            //坐标轴上显示的数据格式设置
            InsertChart.Axes[1].NumberFormat = "0.00%";
            //坐标轴刻度宽度
            //InsertChart.Axes[1].TickLabelSpacing =1; 
            //InsertChart.Axes[1].Title.Position = ChartTitlePositionEnum.chTitlePositionBottom; 

           //给y轴添加一个对应的平行轴    
            InsertChart.Axes.Add(InsertChart.Axes[1].Scaling);  
            InsertChart.Axes[2].Position   =   ChartAxisPositionEnum.chAxisPositionLeft ;
            InsertChart.Axes[2].CrossingAxis = InsertChart.Axes[1];
            InsertChart.Axes[2].HasTitle = true;
            InsertChart.Axes[2].Title.Caption = "金额(元)";

            //绘图区背景颜色
            InsertChart.PlotArea.Interior.Color = "white";

            //图表工作区边框颜色
            InsertChart.Border.Color = "blue";
            //InsertChart.

            //   添加一个series系列
            InsertChart.SeriesCollection.Add(0);

            //  设置次坐标轴,系列2的累积百分比依据次坐标轴填充  
            InsertChart.SeriesCollection.Add(1);
            InsertChart.SeriesCollection[1].Ungroup(true);

            //给定series系列的名字
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);

            //给定分类
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);

            //给定值
            InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);

            //曲线的颜色
            InsertChart.SeriesCollection[1].Line.Color = "blue";

            //绘制第二条图形
            InsertChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
            InsertChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
            InsertChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata1);

           
            //输出文件.
            string strAbsolutePath = (Server.MapPath(".")) + "\\image\\ShowData1.gif";
            laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250);

            //创建GIF文件的相对路径.
            string strRelativePath = "./image/ShowData1.gif";

            //把图片添加到placeholder中,并在页面上显示
            string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
            //this.Image1.ImageUrl = strImageTag;
            this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
        }

        #endregion

    数据库设计:
    自己随便创建一个库,第一列为X轴数据(日期),第二、三列为Y轴数据,随便插入几条记录就可以了。

    请大家指导,如果还有其它什么好的方法,希望大家能共同交流。

    作者:zeke     
              出处:http://zhf.cnblogs.com/
              本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

  • 相关阅读:
    表达式树作为条件封装多表连查
    EF之结构进一步优化
    EF之ExecuteSqlCommand更新出现无效的解决方案
    dynamic与匿名对象
    webapi 通过dynamic 接收可变参数
    EF INNER JOIN,LEFT JOIN,GROUP JOIN
    Linq join on 多条件
    Excel 行列转置 解决竖向拉,字母跟着递增的问题
    Windows7 安装vs2015 之后 调试Web项目IIS启动不了 aspnetcore.dll未能加载
    Mysql 服务在本机,需要单机调试Mysql数据库 发生 不认识hostname‘localhost’
  • 原文地址:https://www.cnblogs.com/ZHF/p/1224065.html
Copyright © 2011-2022 走看看