高人勿入
如图:
我的主要代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ChartOO" xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:Primitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="ChartOO.MainWindow" mc:Ignorable="d" Title="MainWindow" Height="350" Width="800"> <Grid> <chart:Chart Title="test chart" x:Name="chart" > </chart:Chart> </Grid> </Window>
MainWindow.xaml.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.DataVisualization.Charting; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ChartOO { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ViewModel vm = new ViewModel(); this.DataContext = vm; LineSeries series = new LineSeries(); series.Title = "proportion"; series.ItemsSource = vm.List; series.IndependentValuePath = "Key"; series.DependentValuePath = "Proportion"; this.chart.Series.Add(series); } } }
实体类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChartOO { class Model { public int Value { get; set; } public Decimal Proportion { get; set; } public String Key { get; set; } } }
viewModel:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; namespace ChartOO { class ViewModel { public ViewModel() { this.List = new List<Model> { new Model { Key="1",Value= 50,Proportion = 0.1m}, new Model { Key="2",Value= 20,Proportion = 0.2m}, new Model { Key="3",Value= 40,Proportion = 0.5m}, new Model { Key="4",Value= 50,Proportion = 0.1m}, new Model { Key="5",Value= 20,Proportion = 0.2m}, new Model { Key="6",Value= 50,Proportion = 0.8m}, }; } public List<Model> List { get; set; } } }
因为在实际项目中,数据源是动态改变的,所有我没有在该Demo的xaml文件中绑定,而是把绑定代码写在了MainWindow.xaml.cs里面。
你能不能干点有意义的工作啊:
LinearAxis axis = new LinearAxis(); axis.ShowGridLines = false; axis.FontSize = 16; axis.Location = AxisLocation.Left;
LineSeries series = new LineSeries(); series.Title = item.Key; series.ItemsSource = item.Value; series.IndependentValuePath = "Date"; series.DependentValuePath = "Proportion"; series.DependentRangeAxis = axis;
axis.Orientation = AxisOrientation.Y;
https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datavisualization.charting.striplinescollection(v=vs.110).aspx
看在分的份上查了下msdn,我觉得你把StripLinesCollection清空就实现了
msdn说:“StripLinesCollection 类表示 StripLine 对象的集合,实现为 Axis 类的 StripLines 属性”,可是我都没有找到StripLines属性。我设了断点:chart的Axes集合是0,series.IndependentAxis也是null,不知道那个还有那个属性是Axis 类型的了
@会长: 我估计应该是这样写
this.chart.StripLinesCollection.Clear();
然后就实现了
@刘宏玺: 不对,刚刚看了msdn,发现看出属性了,我在研究下
@刘宏玺: 谢谢
@刘宏玺: this.chart.ChartAreas[0].AxisY.StripLines.Clear();
@刘宏玺: 不好意思,你看的类库好像错了,是winfom的chat控件,我用的是wpf toolkit中的chart,不好意思啊,我也没细看你的连接,浪费你时间了。