How do I correctly update my chart values? (In real time)

前端 未结 3 1311
猫巷女王i
猫巷女王i 2021-01-18 11:15

I recently encountered a tool called LiveChart and decided to test it out.

Unfortunately I\'ve been having some problems figuring out how to update the chart values

3条回答
  •  爱一瞬间的悲伤
    2021-01-18 11:45

    Hi for Pie chart you can upload the value in this easy way:

    code Behind:

    /// 
    /// Interaction logic for StatusChart.xaml
    /// 
    public partial class StatusChart : UserControl
    {
        #region - Values -
        private List values;
        public List Values
        {
             
            get => values;
            set
            {
                if (value is null) throw new NullReferenceException();
                if (value.Count > PieChart.Series.Count) throw new IndexOutOfRangeException();
    
                for(int serie = 0; serie < PieChart.Series.Count; serie++)
                {
                    PieChart.Series[serie].ActualValues[0] = values[serie];
                }
    
                PieChart.Series.ForEach((i, s) => s.ActualValues[0] = value[i]);
            }
        }
        #endregion
        public StatusChart()
        {
            InitializeComponent();
            
            values = Enumerable.Repeat(1D, PieChart.Series.Count).ToList();
            
            PointLabel = chartPoint => string.Format(new CultureInfo("en-US"), "{0:#0}s", chartPoint.Y);
    
            GridRoot.DataContext = this;
        }
        public Func PointLabel { get; set; }
    
        /// 
        /// method to enalrge the slice (part of pie chart) 
        /// 
        /// 
        /// 
        private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
        {
            var chart = (LiveCharts.Wpf.PieChart)chartpoint.ChartView;
    
            //clear selected slice.
            foreach (PieSeries series in chart.Series)
                series.PushOut = 0;
    
            var selectedSeries = (PieSeries)chartpoint.SeriesView;
            selectedSeries.PushOut = 7;
        }             
    }
    

    and xaml code:

    
    
    
        
            
                
                
                
    
                
                
    
                
    
                
                
    
                
                
                        
        
    
    

    and just to try the control in a window wpf test put this few lines:

     var temporalSc = Enumerable.Repeat(1D, StatusChart.PieChart.Series.Count).ToList();
                for (var i = 0; i < StatusChart.PieChart.Series.Count; i++)
                {
                    temporalSc[i] += rnd.Next(1, 87);
                }
    
                StatusChart.Values.Clear();
                StatusChart.Values = temporalSc;
    

提交回复
热议问题