gpt4 book ai didi

c# - 存在空点时如何继续图表?

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:28 25 4
gpt4 key购买 nike

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

我的数据是这样的:
sample data

...但是我的图表没有在缺失数据点之间画线:
incomplete chart

如您所见,第 2 列在 2015 年 7 月 9 日的 51 处有一个点,然后点在 2015 年 7 月 16 日之前都是空的。我希望图表从 7/09/2015 到 7/16/2015 绘制一条线。

我怎样才能让它发挥作用?

这是我现有的 Series build :

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

更新 1:
有了@jstreet 的回答,我现在画了一条线。但是该值被表示为 0:
incorrect empty point rendering

预期的是以下粗虚线应替换代表值 0 的线:
expected empty point rendering

更新 2:
修改了for-loop手动添加 DataPoint s 到 series按照@jstreet 在代码隐藏 中添加点的示例:

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.Color = legendColors[header];
series.EmptyPointStyle.AxisLabel = "Empty";

DataTable.Rows
.OfType<DataRow>()
.Select(r => (double)r[header])
.ToList()
.ForEach(v => {
series.Points.Add(new DataPoint(series) {
IsEmpty = v == Double.NaN,
YValues = new double[] { v == Double.NaN ? 0 : v }
});
});

}

...图表仍将本应为空点的点呈现为 0。值(value)。

更新 3:
DataPoint的修改结构|秒。显然v == Double.NaN没有正确评估:

DataTable.Rows
.OfType<DataRow>()
.Select(r => (double)r[header])
.ToList()
.ForEach(v => {
var isEmpty = Double.IsNaN(v);
var value = new double[] { v == Double.NaN ? 0 : v };

series.Points.Add(new DataPoint() {
IsEmpty = isEmpty,
YValues = value
});
});

... 我验证了 Series.Points收藏:
verified series NaN point IsEmpty

最佳答案

需要定义一个EmptyPointStyle,如下图:

            <asp:Series Name="Series1" ChartType="Line" XValueType="DateTime">
<Points>
<asp:DataPoint XValue="42005" YValues="50" />
<asp:DataPoint XValue="42036" YValues="70" />
<asp:DataPoint XValue="42064" YValues="30" />
<asp:DataPoint IsEmpty="True" XValue="42095" YValues="0" />
<asp:DataPoint XValue="42156" YValues="60" />
<asp:DataPoint XValue="42186" YValues="40" />
</Points>
<EmptyPointStyle Color="Red" />
</asp:Series>

结果:

enter image description here

编辑:我还可以使用隐藏代码添加点数:

    protected void Page_Load(object sender, EventArgs e)
{
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(1).ToOADate(), YValues = new double[] { 50 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(2).ToOADate(), YValues = new double[] { 70 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = true, XValue = DateTime.Now.AddDays(3).ToOADate(), YValues = new double[] { 0 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(4).ToOADate(), YValues = new double[] { 30 } });
Chart1.Series[0].Points.Add(new DataPoint { IsEmpty = false, XValue = DateTime.Now.AddDays(5).ToOADate(), YValues = new double[] { 60 } });
}

我没有遇到您描述的无法设置 IsEmpty 或在图表中获取 0 的问题:

enter image description here

关于c# - 存在空点时如何继续图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32061190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com