简体   繁体   English

如何使用Web.UI.DataVisualization.Charting绘制图表?

[英]How to chart using Web.UI.DataVisualization.Charting?

using System.Web.UI.DataVisualization.Charting.Chart

I have data in 2 formats: 我有2种格式的数据:
A. 一种。 垂直格式数据

B. B. 水平格式数据

The data in format A is what I am using to build a chart. 格式A中的数据是我用来构建图表的数据。 But my chart ends up looking like: 但是我的图表最终看起来像: 样本图

I cannot figure out how come no lines are drawn. 我不知道为什么没有画线。 Here is the code I am using: 这是我正在使用的代码:

var dataTable = GetDataTable();
var xAxisTitle = dataTable.Columns[1].ExtendedProperties["Type"].ToString();
var yAxisTitle = dataTable.Columns[0].ExtendedProperties["Type"].ToString();

chart = new Chart() {
    AntiAliasing = AntiAliasingStyles.All,
    TextAntiAliasingQuality = TextAntiAliasingQuality.High,
    Width = 1200,
    Height = 800,
    Enabled = true,
    ForeColor = Color.SaddleBrown
};

var chartArea = new ChartArea() {
    BackColor = Color.White,

    AxisY = new Axis() {
        Enabled = AxisEnabled.True,
        Title = yAxisTitle,
        LineColor = Color.DarkBlue,
        MajorTickMark = new TickMark() {
            Enabled = true,
            LineColor = Color.DarkGreen,
            Interval = .1d
        },
        MinorTickMark = new TickMark() {
            Enabled = true,
            LineColor = Color.Green,
            Interval = .1d
        },
        LabelStyle = new LabelStyle() {
            Enabled = true,
            ForeColor = Color.Red,
            IsEndLabelVisible = true,
            Font = new Font("Calibri", 4, FontStyle.Regular)
        },
        MajorGrid = new Grid() {
            Enabled = true,
            LineColor = Color.LightGray,
            LineWidth = 1
        },
    },

    AxisX = new Axis() {
        Enabled = AxisEnabled.True,
        Title = xAxisTitle,
        LineColor = Color.DarkBlue,
        MajorTickMark = new TickMark() {
            Enabled = true,
            LineColor = Color.Red,
            Interval = .1d
        },
        MinorTickMark = new TickMark() {
            Enabled = true,
            LineColor = Color.DarkGreen,
            Interval = .1d
        },
        LabelStyle = new LabelStyle() {
            Enabled = true,
            ForeColor = Color.Blue,
            IsEndLabelVisible = true,
            Font = new Font("Calibri", 4, FontStyle.Regular)
        },
        MajorGrid = new Grid() {
            Enabled = true,
            LineColor = Color.DarkGray,
            LineWidth = 1
        },
    },
};

chartArea.AxisX.Enabled = AxisEnabled.True;
chartArea.AxisY.Enabled = AxisEnabled.True;
chart.ChartAreas.Add(chartArea);

var lineHeaders = dataTable.Rows
    .OfType<DataRow>()
    .Select(r => r[0].ToString())
    .ToArray();

var i = 0;
for (int column = 0; column < lineHeaders.Length; column++) {
    var header = lineHeaders[column];

    var series = chart.Series[header] = new Series() {
        Enabled = true,
        Name = header,
        Font = new Font("Lucida Sans Unicode", 6f),
        Color = legendColors[header],
        ChartType = SeriesChartType.Line,
        XValueType = ChartValueType.DateTime,
        YValueType = ChartValueType.Double,
    };

    var colData = dataTable.Rows[column]
        .ItemArray
        .Skip(1)
        .Select(d => (double)d)
        .ToArray();

    DataPoint p = new DataPoint() {
        AxisLabel = header,
        XValue = i++,
        YValues = colData,
    };

    series.Points.Add(p);
}

I've also set the data table as the data source on the Chart but there are no data points plotted. 我还将数据表设置为Chart上的数据源,但是没有绘制数据点。

Am I even using the correct data table ...should I be using format B instead? 我什至在使用正确的数据表...我应该使用格式B吗?

I proceeded with the table format option B : 我继续进行表格格式选项B 在此处输入图片说明

Other codes changes were minimal but re-posting the entire snippet below. 其他代码更改很小,但重新发布了以下完整代码段。 The primary fix are these two lines at the end of the method: 主要的解决方法是方法末尾的这两行:

   chart.DataSource = DataTable;
   chart.DataBind();  

工作图

internal void BuildChart(DataTable DataTable) {
    var xAxisTitle = DataTable.Columns[1].ExtendedProperties["Type"].ToString();
    var yAxisTitle = DataTable.Columns[0].ExtendedProperties["Type"].ToString();

    chart = new Chart() {
        AntiAliasing = AntiAliasingStyles.All,
        TextAntiAliasingQuality = TextAntiAliasingQuality.High,
        Width = 1800,
        Height = 500,
        Enabled = true,
        ForeColor = Color.SaddleBrown
    };

    #region build chart area
    var chartArea = new ChartArea() {
        BackColor = Color.White,
        Name = DataTable.TableName,

        AxisY = new Axis() {
            Enabled = AxisEnabled.True,
            Title = yAxisTitle,
            LineColor = Color.DarkBlue,
            MajorTickMark = new TickMark() {
                Enabled = true,
                LineColor = Color.DarkGreen,
            },
            MinorTickMark = new TickMark() {
                Enabled = true,
                LineColor = Color.Green,
            },
            LabelStyle = new LabelStyle() {
                Enabled = true,
                ForeColor = Color.Red,
                IsEndLabelVisible = true,
                Font = new Font("Calibri", 4, FontStyle.Regular)
            },
            MajorGrid = new Grid() {
                Enabled = true,
                LineColor = Color.LightGray,
                LineWidth = 1
            },
        },

        AxisX = new Axis() {
            Enabled = AxisEnabled.True,
            Title = xAxisTitle,
            LineColor = Color.DarkBlue,
            MajorTickMark = new TickMark() {
                Enabled = true,
                LineColor = Color.Red,
            },
            MinorTickMark = new TickMark() {
                Enabled = true,
                LineColor = Color.DarkGreen,
            },
            LabelStyle = new LabelStyle() {
                Enabled = true,
                ForeColor = Color.Blue,
                IsEndLabelVisible = true,
                Font = new Font("Calibri", 4, FontStyle.Regular)
            },
            MajorGrid = new Grid() {
                Enabled = true,
                LineColor = Color.DarkGray,
                LineWidth = 1
            },
        },
    };

    chartArea.AxisX.Enabled = AxisEnabled.True;
    chartArea.AxisY.Enabled = AxisEnabled.True;
    #endregion

    chart.ChartAreas.Add(chartArea);

    var seriesHeaders = DataTable.Columns
        .OfType<DataColumn>()
        .Skip(1)
        .Select(c => c.ColumnName)
        .ToArray();

    chart.Legends.Add(new Legend(DataTable.TableName) {
        Enabled = true
    });

    for (int column = 0; column < seriesHeaders.Length; column++) {
        var header = seriesHeaders[column];

        var series = chart.Series[header] = new Series(header) {
            BorderWidth = 2,
            ChartArea = DataTable.TableName,
            ChartType = SeriesChartType.FastLine,
            Color = legendColors[header],
            Enabled = true,
            Font = new Font("Lucida Sans Unicode", 6f),
            XValueMember = "Week",
            YValueMembers = header
        };

        series.EmptyPointStyle.MarkerColor = legendColors[header];
    }

    chart.DataSource = DataTable;
    chart.DataBind();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以使用System.Web.UI.DataVisualization.Charting旋转烛台图表以水平显示,而不是垂直显示? - Is there a way using System.Web.UI.DataVisualization.Charting to rotate a Candlestick chart to display horizontally instead of vertically? 使用System.Web.UI.DataVisualization.Charting命名空间更改饼图上的标签颜色 - Changing the label color on a Pie Chart using System.Web.UI.DataVisualization.Charting namespace 如何使用System.Web.UI.DataVisualization.Charting - How to work with System.Web.UI.DataVisualization.Charting System.Web.UI.DataVisualization.Charting中的雷达图选择性标签旋转? - Radar chart selective label rotation in System.Web.UI.DataVisualization.Charting? 来自C#System.Web.UI.DataVisualization.Charting.Chart帮助程序的图像映射 - Image map from C# System.Web.UI.DataVisualization.Charting.Chart helper 如何在 .NET Core 应用程序中使用 System.Web.UI.DataVisualization.Charting? - How do I use System.Web.UI.DataVisualization.Charting in .NET Core apps? 如何将System.Web.UI.DataVisualization.Charting dll放入WebMatrix 3? - How do I get System.Web.UI.DataVisualization.Charting dll into WebMatrix 3? iTextSharp将System.Web.UI.DataVisualization.Charting转换为pdf - iTextSharp convert a System.Web.UI.DataVisualization.Charting to pdf IE8上未显示System.Web.UI.DataVisualization.Charting图表 - System.Web.UI.DataVisualization.Charting charts are not displaying on IE8 System.Web.UI.DataVisualization.Charting .net 4.0 中缺少错误? - System.Web.UI.DataVisualization.Charting missing error in .net 4.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM