System.Web.UI.DataVisualization.Charting和Rangebar图表集成

时间:2015-08-18 12:36:49

标签: asp.net-mvc charts bar-chart dotnetcharting

通过使用以下代码,我的图表看起来像代码之后的图像。

我的问题是现在条形和y轴点未正确对齐。

如何在同一行显示范围栏及其对应的y轴值?

List<dummyGraph> objGraphList = new List<dummyGraph>();
        dummyGraph objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Underwriter";
        objDummyGraph.min = 1;
        objDummyGraph.max = 8;
        objDummyGraph.Median = 1;
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Construction";
        objDummyGraph.min = 2;
        objDummyGraph.max = 5;
        objDummyGraph.Median = 2;
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Design";
        objDummyGraph.min = 3;
        objDummyGraph.max = 6;
        objDummyGraph.Median = 3;
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Partner";
        objDummyGraph.min = 4;
        objDummyGraph.max = 6;
        objDummyGraph.Median = 3;
        objGraphList.Add(objDummyGraph);

        System.Web.UI.DataVisualization.Charting.Chart chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
        System.Web.UI.DataVisualization.Charting.ChartArea chartArea1 = new System.Web.UI.DataVisualization.Charting.ChartArea();
        System.Web.UI.DataVisualization.Charting.Legend legend1 = new System.Web.UI.DataVisualization.Charting.Legend();
        System.Web.UI.DataVisualization.Charting.Series seriesInstance;
        List<System.Web.UI.DataVisualization.Charting.Series> seriesList = new List<System.Web.UI.DataVisualization.Charting.Series>();

        int xordinal = 0;

        foreach (var i in objGraphList)
        {
            seriesInstance = new System.Web.UI.DataVisualization.Charting.Series(i.TKFEType);

            seriesInstance.ChartType = SeriesChartType.RangeBar;
            seriesInstance.Color = Color.SkyBlue;
            seriesInstance.YValuesPerPoint = 2;
            seriesInstance.AxisLabel = i.TKFEType;

            double yplot1 = (double)i.min;
            double yplot2 = (double)i.max;

            seriesInstance.IsValueShownAsLabel = true;

            xordinal = xordinal + 1;
            seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);                

            chart1.Series.Add(seriesInstance);
        }

        chart1.ChartAreas.Add(chartArea1);
        chart1.ChartAreas[0].AxisY.Interval = 1;

        chartArea1 = chart1.ChartAreas[0];

        chartArea1.AxisY.IsStartedFromZero = true;
        chartArea1.AxisY.Enabled = AxisEnabled.False;


        chartArea1.AlignmentOrientation = AreaAlignmentOrientations.All;

        using (MemoryStream ms = new MemoryStream())
        {
            chart1.SaveImage(ms, ChartImageFormat.Png);
            return File(ms.ToArray(), "image/png");
        }

enter image description here

样条图疑问

我有一个样条图。我需要显示从x(例如:Underwriter)到y(例如:200)的直线,如下图所示。我该怎么做?

enter image description here

更新后的代码:

  public ActionResult MMBRChart()
    {
        //Area for generating line chart
        List<dummyGraph> objGraphList = new List<dummyGraph>();
        dummyGraph objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Underwriter";
        objDummyGraph.min = 1;
        objDummyGraph.max = 8;
        objDummyGraph.Median = "201";
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Construction";
        objDummyGraph.min = 2;
        objDummyGraph.max = 5;
        objDummyGraph.Median = "187";
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Design";
        objDummyGraph.min = 3;
        objDummyGraph.max = 6;
        objDummyGraph.Median = "243";
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Partner";
        objDummyGraph.min = 4;
        objDummyGraph.max = 6;
        objDummyGraph.Median = "200";
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Auditor";
        objDummyGraph.min = 4;
        objDummyGraph.max = 6;
        objDummyGraph.Median = "29";
        objGraphList.Add(objDummyGraph);

        objDummyGraph = new dummyGraph();
        objDummyGraph.TKFEType = "Associate";
        objDummyGraph.min = 4;
        objDummyGraph.max = 6;
        objDummyGraph.Median = "198";
        objGraphList.Add(objDummyGraph);

        System.Web.UI.DataVisualization.Charting.Chart chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
        System.Web.UI.DataVisualization.Charting.Chart lineChart1 = new System.Web.UI.DataVisualization.Charting.Chart();
        System.Web.UI.DataVisualization.Charting.ChartArea chartArea1 = new System.Web.UI.DataVisualization.Charting.ChartArea();
        System.Web.UI.DataVisualization.Charting.ChartArea linechartArea1 = new System.Web.UI.DataVisualization.Charting.ChartArea("lineChart");
        System.Web.UI.DataVisualization.Charting.Legend legend1 = new System.Web.UI.DataVisualization.Charting.Legend();
        System.Web.UI.DataVisualization.Charting.Series seriesInstance = new Series();

        chart1.Titles.Add("Median MBR");//Add chart title 
        chart1.Series.Add(seriesInstance);
        chart1.Series[0].Points.DataBindXY(objGraphList.Select(x => x.TKFEType).ToList(), objGraphList.Select(x => x.Median).ToList());
        chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Spline;
        chart1.Series[0].Color = Color.SkyBlue;
        chart1.Series[0].BorderWidth = 2;
        chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
        chart1.Series[0].MarkerColor = Color.Blue;
        chart1.Series[0].Name = "MyGraph";

        seriesInstance = new System.Web.UI.DataVisualization.Charting.Series("lineSeries");
        //seriesInstance.ChartType = SeriesChartType.Bar;//Set chart type here
        seriesInstance.Color = Color.SkyBlue;
        seriesInstance.YValuesPerPoint = 2;

        chart1.Series.Add(seriesInstance);

        int j = 0;

        foreach (var p in objGraphList)
        {
            //chart1.Series[1].Points.AddXY(j, p.max);
            j = j + 1;
            chart1.Series[1].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar;
            chart1.Series[1].Points.AddXY(p.TKFEType, p.Median);
            chart1.Series[1].Color = Color.Green;
            chart1.Series[1].BorderWidth = 2;
            chart1.Series[1].ToolTip = "#VALY, #VALX";
        }


        chart1.ChartAreas.Add(chartArea1);

        chart1.ChartAreas[0].Area3DStyle.Enable3D = false;
        chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.FromArgb(50, 200, 200, 200);
        chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(50, 200, 200, 200);

        using (MemoryStream ms = new MemoryStream())
        {
            chart1.SaveImage(ms, ChartImageFormat.Png);
            return File(ms.ToArray(), "image/png");
        }
    }

1 个答案:

答案 0 :(得分:1)

您要在图表中添加多个系列。不确定那是你的意图。此外,您使用TKFEType作为标签,而不是轴的实际值。这些因素加在一起可能会错误地调整您的图表。以下是我的建议:

        List<DummyGraph> list1 = new List<DummyGraph>();
        list1.Add(new DummyGraph { TKFEType = "Underwritter", Min = 1, Max = 8, Median = 1 });
        list1.Add(new DummyGraph { TKFEType = "Construction", Min = 2, Max = 5, Median = 2 });
        list1.Add(new DummyGraph { TKFEType = "Design", Min = 3, Max = 6, Median = 3 });
        list1.Add(new DummyGraph { TKFEType = "Partner", Min = 4, Max = 6, Median = 4 });

        Chart1.DataSource = list1;
        Chart1.Series[0].XValueMember = "TKFEType";
        Chart1.Series[0].YValueMembers = "Min, Max";
        Chart1.DataBind();

enter image description here