タイトル位置の微調整

OxyThickness では、マージンの調整はできますが、タイトル自体を左上に寄せるには制限があります。OxyPlotには標準的にタイトル位置を柔軟に変更する機能が少ないため、完全に左上に移動するためには以下の方法が最も現実的です。

  1. 標準のタイトルを非表示にする
  2. TextAnnotation を使って任意の位置にタイトルを表示する

以下に、この方法でタイトルを左上に配置するコードを示します。

var plotModel = new PlotModel();
plotModel.Title = null; // 標準のタイトルを非表示

// 左上にカスタムタイトルを配置
var titleAnnotation = new TextAnnotation
{
    Text = "Your Title",
    TextPosition = new DataPoint(plotModel.Axes[0].ActualMinimum, plotModel.Axes[1].ActualMaximum), // X軸最小値とY軸最大値を利用
    TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left,
    TextVerticalAlignment = OxyPlot.VerticalAlignment.Top,
    FontSize = 16, // 必要に応じてフォントサイズを調整
    StrokeThickness = 0 // 枠線を非表示
};

// カスタムタイトルを追加
plotModel.Annotations.Add(titleAnnotation);

TextPosition の座標 (DataPoint) を調整することで、グラフの左上の理想的な位置に配置します。

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:oxy="http://oxyplot.org/wpf"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!-- 行の定義 -->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/> <!-- タイトル用 -->
            <RowDefinition Height="*"/>    <!-- グラフ用 -->
        </Grid.RowDefinitions>

        <!-- 左上にタイトルを表示 -->
        <TextBlock Text="Your Title"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Margin="10,10,0,10"
                   FontSize="16"
                   FontWeight="Bold"
                   Foreground="Black"
                   Grid.Row="0" /> <!-- 上部の行に配置 -->

        <!-- グラフ -->
        <oxy:PlotView x:Name="plotView"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch"
                      Grid.Row="1" /> <!-- 下部の行に配置 -->
    </Grid>
</Window>

コメント

タイトルとURLをコピーしました