简体   繁体   中英

Add two lists as x and y coordinates in WPF Toolkit graph

I have two lists:

Temperature = new List<string>(reader.GetTemperature());
Time = new List<string>(reader.GetTime());

The Time list is the x-coordinates and the Temperature list is the y-coordinates.

I have tried the following without any luck:

private void LoadDataChart()
{
     List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();

     foreach (string s in Temperature)
     {
          data.Add(new KeyValuePair<string, string>(s, null));    
     }

     foreach (string t in Time)
     {
          data.Add(new KeyValuePair<string, string>(null, t));
     }

     dataChart.DataContext = data;
}

With the following XAML:

<chartingToolkit:Chart x:Name="dataChart" HorizontalAlignment="Left" Margin="342,84,0,0" Title="" VerticalAlignment="Top" Height="300" Width="565" IsEnabled="False" LegendTitle="">
     <chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="False" />
</chartingToolkit:Chart>

Can anybody help?

Try the below code. I Hope you are trying to do.

 <wpfTool:Chart x:Name="dataChart" HorizontalAlignment="Left" Margin="342,84,0,0" Title="" VerticalAlignment="Top" Height="300" Width="565" IsEnabled="False" LegendTitle="">
        <wpfTool:LineSeries DependentValuePath="Value" IndependentValuePath="Key" x:Name="series" IsSelectionEnabled="False" />
    </wpfTool:Chart>
public partial class Window3 : Window
{
    public Window3()
    {
        InitializeComponent();
        LoadColumnChartData();
    }

    List<string> Temperature = new List<string>() { "60", "65" };
    List<string> Time = new List<string>() { "1/28/2015 12:00:00", "1/28/2015 2:20:35" };

    private void LoadColumnChartData()
    {
        int countTemp = Temperature.Count;
        int countTime = Time.Count;
        List<KeyValuePair<DateTime, double>> data1 = new List<KeyValuePair<DateTime, double>>();

        if(countTemp==countTime)
        {
            for (int i = 0; i < countTemp; i++)
            {
                KeyValuePair<DateTime, double> dt = new KeyValuePair<DateTime, double>(DateTime.Parse(Time[i]),Convert.ToDouble(Temperature[i]));                  
                data1.Add(dt);
            }
        }           

        series.ItemsSource = data1;
    }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM