作成日: 2021/04/19 最終更新日: 2021/04/19
文書種別
使用方法
詳細
FlexChartでは、凡例のタイトルを設定するためにLegendTitleプロパティが用意されています。そのため、リソースファイルで「キー名」に「LegendTitle」を、「値」に任意のローカライズ文字列を設定することで、凡例のテキストをローカライズできます。
【XAML】
<c1:C1FlexChart LegendTitle="{x:Static res:Resources.LegendTitle}">
</code>
【Resources.ja-JP.resx】
(名前) (値)
"LegendTitle" "凡例のタイトル"
一方、凡例の各項目には自動的にシリーズ名が表示されるので、凡例項目としてのプロパティは用意されておらず、凡例タイトルのような方法でこれらをローカライズすることはできません。
対応策として、ISeriesのGetLegendItemNameメソッドを実装したシリーズの拡張クラスを使用する方法が考えられます。このクラス内で、ResourceManagerのGetStringメソッドにて凡例の各項目のローカライズ文字列を取得するようGetLegendItemNameメソッドを拡張し、得られたテキストを凡例の各項目として表示します。
以下に、この方法を実装したサンプルコード全体を記載します。
【Resources.ja.resx】
(名前) (値)
"LegendTitle" "凡例のタイトル"
"Series1" "シリーズ1"
"Series2" "シリーズ2"
【XAML】
<Window x:Class="FlexChartLegend.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:res="clr-namespace:FlexChartLegend.Resources"
xmlns:local="clr-namespace:FlexChartLegend" xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<c1:C1FlexChart x:Name="flexChart" ChartType="LineSymbols" LegendTitle="{x:Static res:Resources.LegendTitle}">
<local:SeriesExtended x:Name="series2" Binding="Y" BindingX="X" ResourceKey="Series2"></local:SeriesExtended>
</c1:C1FlexChart>
</Grid>
</Window>
【Code】
using C1.Chart;
using C1.WPF.Chart;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
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;
namespace FlexChartLegend
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// code behind
flexChart.Series.Add(new SeriesExtended()
{
ResourceKey = "Series1",
Binding = "Y",
BindingX = "X",
ItemsSource = new List<Point>()
{
new Point(5, 12),
new Point(6, 18),
new Point(7, 10)
}
});
// resource key set in xaml
series2.ItemsSource = new List<Point>()
{
new Point(5, 20),
new Point(6, 11),
new Point(7, 20)
};
}
}
public class SeriesExtended : Series, ISeries
{
private ResourceManager _manager;
/// <summary>
/// Name of the key specified in resource file for series name.
/// </summary>
public string ResourceKey { get; set; }
public SeriesExtended()
{
_manager = new ResourceManager("FlexChartLegend.Resources.Resources", Assembly.GetExecutingAssembly());
}
public string GetLegendItemName(int index)
{
if (string.IsNullOrEmpty(ResourceKey))
return SeriesName;
return _manager.GetString(ResourceKey);
}
}
}