動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
処理内容
- 凡例を表示する
実装方法
参考: Basic Line Chart @ LiveCharts
- XAML
- lvc:CartesianChart に対して「LegendLocation」を指定する
- code behind
- LineSeriesなどに「Title」を追加する
code
MainWindow.xaml
<Window x:Class="_171124_t1600_legend.MainWindow"
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:_171124_t1600_legend"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<lvc:CartesianChart Series="{Binding seriesCollection}" Height="250"
LegendLocation="Right"/>
</StackPanel>
</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.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;
// 以下を追加
using LiveCharts;
using LiveCharts.Wpf;
namespace _171124_t1600_legend
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
graph_init();
}
private void graph_init()
{
gd = new GraphData();
var sc = new SeriesCollection {
new LineSeries
{
Title = "pi",
Values = new ChartValues<double> { 3, 1, 4, 1, 5, 9, 2 }
},
new LineSeries
{
Title = "napier",
Values = new ChartValues<double> { 2, 7, 1, 8, 2, 8, 1, 8 }
}
};
gd.seriesCollection = sc;
this.DataContext = gd;
}
GraphData gd;
public class GraphData
{
public SeriesCollection seriesCollection { get; set; }
}
}
}
検索用キーワード
- legend