我是C#的新手。使用图表并尝试从我的绘制的lineseries中删除标记。
这是代码行:
private void LoadLineChartData()
{
LineSeries ls1 = new LineSeries();
ls1.Title = "Title1";
ls1.IndependentValueBinding = new Binding("Key");
ls1.DependentValueBinding = new Binding("Value");
ls1.ItemsSource =
new KeyValuePair<int, int>[]{
new KeyValuePair<int,int>(1, 100),
new KeyValuePair<int,int>(2, 130),
new KeyValuePair<int,int>(3, 150),
new KeyValuePair<int,int>(4, 125),
new KeyValuePair<int,int>(5,155) };
MyChart.Series.Add(ls1);
ls1.MarkerStyle = MarkerStyle.None;
}
它不起作用,这是错误: 'System.Windows.Controls.DataVisualization.Charting.LineSeries'不包含'MarkerStyle'的定义,也没有扩展方法'MarkerStyle'接受类型'
的第一个参数我是否使用错误的.dll作为图表的参考?什么是正确的?
答案 0 :(得分:0)
当您应该引用System.Windows.Forms时,您正在引用System.Windows.Control。
在您的项目中,右键单击“引用”并添加对以下内容的引用:System.Windows.Forms和System.Windows.Form.DataVisualization 。
在引用正确的程序集后,将代码更改为:
// Declare the following usings
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;
using System.Collections.Generic;
...
private void LoadLineChartData()
{
Chart myChart = new Chart();
myChart.Series.Add("ls1");
myChart.Series["ls1"].ChartType = SeriesChartType.Line;
myChart.Series["ls1"].MarkerStyle = MarkerStyle.None;
KeyValuePair<int, int>[] pairs =
{
new KeyValuePair<int, int>(1, 100),
new KeyValuePair<int, int>(2, 130),
new KeyValuePair<int, int>(3, 150),
new KeyValuePair<int, int>(4, 125),
new KeyValuePair<int, int>(5, 155)
};
foreach (var pair in pairs)
myChart.Series["ls1"].Points.AddXY(pair.Key, pair.Value);
}