【问题标题】:How to chart using Web.UI.DataVisualization.Charting?如何使用 Web.UI.DataVisualization.Charting 绘制图表?
【发布时间】:2015-08-17 16:58:18
【问题描述】:

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

我有两种格式的数据:
A.

B.

A 格式的数据是我用来构建图表的数据。但我的图表最终看起来像:

我无法弄清楚为什么没有画线。这是我正在使用的代码:

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);
}

我还在Chart 上将数据表设置为数据源,但没有绘制数据点。

我是否使用了正确的数据表...我应该改用 B 格式吗?

【问题讨论】:

  • 显然您只为每个系列添加了一个点。因为是折线图,所以看不到。将其更改为条形图,每个系列应该只能看到一个条形图。

标签: c# charts linechart


【解决方案1】:

我继续使用表格格式选项B

其他代码更改很小,但在下面重新发布了整个 sn-p。 primary 修复是方法末尾的这两行:

   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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2011-02-26
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多