microcharts-dotnet / Microcharts

Create cross-platform (Xamarin, Windows, ...) simple charts.
MIT License
2.03k stars 360 forks source link

Help!~ I want to refresh my LineChart by a button #153

Open zzl4883a opened 5 years ago

zzl4883a commented 5 years ago

I have a problem for help~ I want to change the LineChart view dynamic. I put a button,the button's clicked_method called "CreateChart",and this method passes two parameters(double [] dataX and double [] dataY).
I want to change the LineChart view when I click the button pass different parameters(dataX and dataY). There are two pieces of code at the bottom,the pieces 1 code can show lineChart normal pieces 1: ` public MainPage() {

        InitializeComponent();
        //lblResult.BindingContext = resultsStr; //绑定一个关联的实例变量
        //lblResult.SetBinding(text, "Value"); // 宿主设定 自身属性 与 被绑定 的对象的属性
        // BindingContext = new viewModel.MPViewModel();
        var entries = new[]
        {
             new Microcharts.Entry(200)
            {
                Label = "10",
                ValueLabel = "200",
                Color = SKColor.Parse("#45d758")
            },
              new Microcharts.Entry(200)
            {
                Label = "1000",
                ValueLabel = "200",
                Color = SKColor.Parse("#45d758")
            }
        };
        LineChart lineChart = new LineChart()
        {
            Entries = entries,
            LineMode = LineMode.Straight,
            LineSize = 4
        };
        this.chartView.Chart = lineChart;

    }`

pieces2: public void CreateChart(double [] dataX,double [] dataY) { if (dataX.Length == dataY.Length) { List<Microcharts.Entry> entryList = new List<Microcharts.Entry>(); for (int i = 0; i < dataX.Length; i++) { var entry = new Microcharts.Entry(float.Parse(dataY[i].ToString())) { Label = $"{ dataX[i].ToString()}", ValueLabel = $"{dataY[i].ToString()}", Color = SKColor.Parse("#45d758") }; entryList.Add(entry); } var entries = entryList.ToArray(); LineChart lineChart = new LineChart() { Entries = entries, LineMode = LineMode.Straight, LineSize = 4 }; Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { this.chartView.Chart = lineChart; }); }

                                                                                                                                  _thanks a lot_ 
zzl4883a commented 5 years ago

pieces2: public void CreateChart(double[] dataX, double[] dataY) { if (dataX.Length == dataY.Length) { List<Microcharts.Entry> entryList = new List<Microcharts.Entry>(); for (int i = 0; i < dataX.Length; i++) { var entry = new Microcharts.Entry(float.Parse(dataY[i].ToString())) { Label = $"{ dataX[i].ToString()}", ValueLabel = $"{dataY[i].ToString()}", Color = SKColor.Parse("#45d758") }; entryList.Add(entry); } var entries = entryList.ToArray(); LineChart lineChart = new LineChart() { Entries = entries, LineMode = LineMode.Straight, LineSize = 4 }; Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { this.chartView.Chart = lineChart; }); } }