zoukankan      html  css  js  c++  java
  • Dev Express图表(折线图双折线)

    实现双折线时,X轴显示在两条Y轴的0下标处

    将页面中放入报表标签

        

    在后台load或者按钮点击事件中获取图表的数据集DataTable直接调用

    CreateChart(dt);绘制折线图

    当前绘制的图表ID 是WebChartControl1 

    /// <summary>
    /// 根据数据创建一个图形展现
    /// </summary>
    /// <param name="caption">图形标题</param>
    /// <param name="viewType">图形类型</param>
    /// <param name="dt">数据DataTable</param>
    /// <param name="rowIndex">图形数据的列名</param>
    /// <returns></returns>
    private Series CreateSeries(string caption, ViewType viewType, DataTable dt, string CluName)
    {

    //Dt中的Month列为X轴
    Series series = new Series(caption, viewType);
    series.ArgumentScaleType = ScaleType.Qualitative;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    string argument = dt.Rows[i]["Month"].ToString();//参数名称
    SeriesPoint point;
    if (dt.Rows[i][CluName].ToString() != "")
    {
    decimal value = (decimal)dt.Rows[i][CluName];//参数值
    if (CluName == "SalTrendAgent")
    {
    point = new SeriesPoint(argument, value * 100, 0);
    point.IsEmpty = false;
    series.Points.Add(point);
    }
    if (CluName == "SalTrend")
    {
    point = new SeriesPoint(argument, value * 100, 1);
    point.IsEmpty = false;
    series.Points.Add(point);
    }
    }
    else
    {
    point = new SeriesPoint(argument, 0, 0);
    point.IsEmpty = true;
    series.Points.Add(point);

    }

    }
    return series;
    }
    /// <summary>
    /// 创建Series
    /// </summary>
    /// <param name="dt">图表数据源</param>
    private void CreateChart(DataTable dt)
    {

    #region Series

    Series series1 = CreateSeries("趋势A(%)", ViewType.Line, dt, "列名1");
    //创建几个图形的对象
    Series series2 = CreateSeries("趋势Y(%)", ViewType.Line, dt, "列名2");
    #endregion

    WebChartControl1.Series.Add(series1);

    //((LineSeriesView)series1.View).AxisY.Title.Text = series1.Name;
    //((LineSeriesView)series1.View).AxisY.Title.Alignment = StringAlignment.Far; //顶部对齐
    ((LineSeriesView)series1.View).AxisY.Title.Visible = false; //显示标题
    ((LineSeriesView)series1.View).AxisY.Title.Font = new Font("宋体", 12.0f, FontStyle.Bold);
    Color color = Color.Blue;//设置坐标的颜色和图表线条颜色一致
    series1.View.Color = color;
    ((LineSeriesView)series1.View).AxisY.Title.TextColor = color;
    ((LineSeriesView)series1.View).AxisY.Label.TextColor = color;
    ((LineSeriesView)series1.View).AxisY.Label.EndText = "%";
    ((LineSeriesView)series1.View).AxisY.Color = color;

    WebChartControl1.Series.Add(series2);
    series2.View.Color = Color.Green;

    CreateAxisY(series2);


    //设置X轴和Y轴的格式  
    //((LineSeriesView)series1.View).AxisX.Alignment = AxisAlignment.Zero;
    ((LineSeriesView)series1.View).AxisX.MinorCount = 1;
    WebChartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside;
    WebChartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.Top;
    WebChartControl1.ShowLoadingPanel = true;
    series1.Label.Visible = false;
    series2.Label.Visible = false;
    //((PointSeriesView)series1.View).AxisX.Label.Angle = 320;
    ((PointSeriesView)series1.View).AxisX.Alignment = AxisAlignment.Zero;
    ((PointSeriesView)series1.View).AxisX.GridLines.Visible = true;
    ((PointSeriesView)series1.View).AxisX.Label.Visible = false;//隐藏当前X轴 只显示当前的X轴的文字Label
    decimal y1Max = Convert.ToDecimal(((PointSeriesView)series1.View).AxisY.Range.MaxValue);
    decimal y1Min = Convert.ToDecimal(((PointSeriesView)series1.View).AxisY.Range.MinValue);

    //实现两条Y轴的原点在一条直线上
    if (y1Min < 0)
    {
    if (Math.Abs(y1Max) > Math.Abs(y1Min))
    {
    ((PointSeriesView)series1.View).AxisY.Range.MaxValue = Math.Abs(y1Max);
    ((PointSeriesView)series1.View).AxisY.Range.MinValue = 0 - y1Max;

    }
    else
    {
    ((PointSeriesView)series1.View).AxisY.Range.MaxValue = Math.Abs(y1Min);
    ((PointSeriesView)series1.View).AxisY.Range.MinValue = 0 - Math.Abs(y1Min);

    }
    decimal y2Max = Convert.ToDecimal(((PointSeriesView)series2.View).AxisY.Range.MaxValue);
    ((PointSeriesView)series2.View).AxisY.Range.MinValue = 0 - y2Max;

    }
    else
    {
    ((PointSeriesView)series1.View).AxisY.Range.MinValue = 0;
    ((PointSeriesView)series2.View).AxisY.Range.MinValue = 0;
    }

    ChartTitle ct = new ChartTitle();
    ct.Text = cmb_Flog.SelectedItem.Text + "可比趋势";
    WebChartControl1.Titles.Add(ct);
    #region 创建第二条X轴
    XYDiagram diagram = (XYDiagram)WebChartControl1.Diagram;
    //清除diagram的所有第二X轴
    diagram.SecondaryAxesX.Clear();
    //定义一新X轴
    SecondaryAxisX secondaxisx = new SecondaryAxisX("axisX");
    //定义新X轴的标题
    secondaxisx.Label.Angle = 320;
    secondaxisx.Title.Visible = false;
    secondaxisx.Alignment = AxisAlignment.Near;
    secondaxisx.Color = Color.White;
    secondaxisx.MinorCount = 1;
    //图标第二X坐标集合中加入该新X轴
    diagram.SecondaryAxesX.Add(secondaxisx);
    //替换默认坐标轴
    ((LineSeriesView)series2.View).AxisX = secondaxisx;
    #endregion
    }
    /// <summary>
    /// 创建图表的第二坐标系
    /// </summary>
    /// <param name="series">Series对象</param>
    /// <returns></returns>
    private SecondaryAxisY CreateAxisY(Series series)
    {
    SecondaryAxisY myAxis = new SecondaryAxisY(series.Name);
    ((XYDiagram)WebChartControl1.Diagram).SecondaryAxesY.Add(myAxis);
    ((LineSeriesView)series.View).AxisY = myAxis;
    myAxis.Title.Text = series.Name;
    myAxis.Title.Alignment = StringAlignment.Far; //顶部对齐
    myAxis.Title.Visible = false; //显示标题
    myAxis.Title.Font = new Font("宋体", 12.0f, FontStyle.Bold);
    Color color = series.View.Color;//设置坐标的颜色和图表线条颜色一致
    myAxis.Title.TextColor = color;
    myAxis.Label.TextColor = color;
    myAxis.Label.EndText = "%";
    myAxis.Color = color;
    return myAxis;
    }

    。。。。如有不足之处望指教!!!!!

  • 相关阅读:
    URI、URL、URN区别
    http历史
    http基础
    那些年我踩过的electron+react的坑!!!
    【Error】System limit for number of file watchers reached
    zsh: corrupt history file /home/floodlight/.zsh_history
    electron中持久化保存数据的解决方案electron-store
    坑:pytest 运行报错unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin'
    坑:找到LoadRunner中Recording Options和 Run Time Settings配置选项确实的原因
    Loadrunner基本概念解析<一>
  • 原文地址:https://www.cnblogs.com/moyelinfei/p/5016474.html
Copyright © 2011-2022 走看看