C# 更改线条系列的颜色
希望有人能帮忙。到目前为止,我已经花了大约4小时的时间尝试我在论坛上找到的例子,但都没有用。下面是我的XAML代码;我认为有一种简单的方法可以插入一个参数来设置颜色,但我还没有找到一种有效的方法 我也尝试过代码隐藏;我发现的所有例子都没有改变任何事情C# 更改线条系列的颜色,c#,wpf,xaml,C#,Wpf,Xaml,希望有人能帮忙。到目前为止,我已经花了大约4小时的时间尝试我在论坛上找到的例子,但都没有用。下面是我的XAML代码;我认为有一种简单的方法可以插入一个参数来设置颜色,但我还没有找到一种有效的方法 我也尝试过代码隐藏;我发现的所有例子都没有改变任何事情 <Grid> <charting:Chart Name="lineChart" Title="Stock"
<Grid>
<charting:Chart Name="lineChart"
Title="Stock"
VerticalAlignment="Top"
Margin="0,10,10,0"
Height="550">
<charting:LineSeries Name="Price"
Title="Price"
DependentValuePath="Value"
IndependentValuePath="Key"
ItemsSource="{Binding [0]}"
IsSelectionEnabled="True"/>
<charting:LineSeries Name="SMA50"
Title="50 SMA"
DependentValuePath="Value"
IndependentValuePath="Key"
ItemsSource="{Binding [1]}"
IsSelectionEnabled="True"/>
<charting:LineSeries Name="SMA200"
Title="200 SMA"
DependentValuePath="Value"
IndependentValuePath="Key"
ItemsSource="{Binding [2]}"
IsSelectionEnabled="True"/>
</charting:Chart>
</Grid>
这是我调用窗口的代码
private void bGraph_Click(object sender, RoutedEventArgs e)
{
Graph g = new Graph();
g.Show();
List<KeyValuePair<DateTime, int>> listPrice = new List<KeyValuePair<DateTime, int>>();
List<KeyValuePair<DateTime, int>> listSMA50 = new List<KeyValuePair<DateTime, int>>();
List<KeyValuePair<DateTime, int>> listSMA200 = new List<KeyValuePair<DateTime, int>>();
DateTime d = new DateTime(2000,1,1);
for (int i = 1; i < 10; i++)
{
listPrice.Add(new KeyValuePair<DateTime, int>(d, i));
listSMA50.Add(new KeyValuePair<DateTime, int>(d, i*2));
listSMA200.Add(new KeyValuePair<DateTime, int>(d, i * 3));
d = d.AddDays(1.0);
}
var dataSourceList = new List<List<KeyValuePair<DateTime, int>>>();
dataSourceList.Add(listPrice);
dataSourceList.Add(listSMA50);
dataSourceList.Add(listSMA200);
g.lineChart.DataContext = dataSourceList;
}
private void bGraph\u单击(对象发送方,路由目标)
{
图g=新图();
g、 Show();
列表价格=新列表();
List listSMA50=新列表();
List listSMA200=新列表();
日期时间d=新的日期时间(2000,1,1);
对于(int i=1;i<10;i++)
{
添加(新的KeyValuePair(d,i));
listSMA50.Add(新的键值对(d,i*2));
添加(新的键值对(d,i*3));
d=d.add天(1.0);
}
var dataSourceList=新列表();
dataSourceList.Add(listPrice);
dataSourceList.Add(listSMA50);
dataSourceList.Add(listSMA200);
g、 lineChart.DataContext=dataSourceList;
}
任何帮助都会很棒。在我看来,windows窗体版本的图表比WPF版本简单得多。我没有图表方面的经验,但我想提供帮助 有几件事看起来很奇怪
您可以使用DataPointStyle属性指定它:
<charting:LineSeries Name="Price"
Title="Price"
DependentValuePath="Value"
IndependentValuePath="Key"
ItemsSource="{Binding [0]}"
IsSelectionEnabled="True"
DataPointStyle="{StaticResource myDataPointStyle}" />
此图表名称空间来自哪个库?我正在使用system.windows.controls.datavisualization.ToolKit。Graph来自哪个名称空间?Graph是我的窗口的名称。这是完整的XAML代码,谢谢您的关注。我的应用程序有两个窗口,主窗口和此窗口图。单击主窗口中的按钮bGraph_Click将调用应用程序打开一个新窗口。一切正常,数据显示;除了我似乎无法更改图形的线条颜色之外。@ErnieCooper啊,对不起,你是想在运行时更改颜色吗?@ErnieCooper我可以更改宽度,但颜色不会更改,这似乎是控件中的一个错误。非常感谢Mark;我得到了你给我的xaml示例。花了一点时间弄清楚如何将样式插入(used Window.Resources)。我无法让c代码示例正常工作;看起来LineDataPoint属于表单名称空间,我正在做一个WPF示例。有没有一种方法可以在我的WPF程序中使用它,还是只用于表单?谢谢你帮助我站起来跑步;动态原因可能是额外的,但并不重要。听起来不对,您是否意外引用了该库的表单和wpf版本?我通过NuGet添加了Toolkit,当我将光标放在源文件中的“LineDataPoint”上并点击F12时,元数据文件的第二行是
//C:\a\u local\u folder\WPFToolkit.DataVisualization.3.5.50211.1\lib\System.Windows.Controls.DataVisualization.Toolkit.dll“
我没有添加使用system.windows.controls.DataVisualization.charting。。。。现在这样做了,它就成功了。愚蠢的省略。。。。谢谢
private void bGraph_Click(object sender, RoutedEventArgs e)
{
var style = new Style(typeof(Polyline));
style.Setters.Add(new Setter(Polyline.StrokeProperty, Brushes.Red));
style.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 5.0));
((LineSeries) lineChart.Series[0]).PolylineStyle = style;
((LineSeries) lineChart.Series[1]).PolylineStyle = style;
((LineSeries) lineChart.Series[2]).PolylineStyle = style;
}
<charting:LineSeries Name="Price"
Title="Price"
DependentValuePath="Value"
IndependentValuePath="Key"
ItemsSource="{Binding [0]}"
IsSelectionEnabled="True"
DataPointStyle="{StaticResource myDataPointStyle}" />
<Style x:Key="myDataPointStyle" TargetType="{x:Type charting:LineDataPoint}">
<Setter Property="Background" Value="Blue"/>
</Style>
var style = new Style();
style.TargetType = typeof(LineDataPoint);
style.Setters.Add(new Setter(BackgroundProperty, Brushes.Blue));
this.Price.DataPointStyle = style;