How to remove space between WPF Toolkit chart area and plot area?

后端 未结 2 1206
再見小時候
再見小時候 2021-02-13 20:45

I am using chartingToolKit:Chart control. I want to remove the white space appear in between the chart and plot area. Attached the WPF sample and image of area to be removed.

相关标签:
2条回答
  • 2021-02-13 21:25

    To give you a little more space without re-templating the control, set the Margin (as you did) and Padding of the chart control to zero.

    Margin="0" Padding="0"
    
    0 讨论(0)
  • 2021-02-13 21:30

    In order to achieve this you need to re-template the chart. The standard chart template is as follows:

                <ControlTemplate TargetType="charting:Chart">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
    
                            <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />
    
                            <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                            <Grid Grid.Row="1" Margin="0,15,0,15">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
    
                                <datavis:Legend x:Name="Legend" Title="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" Grid.Column="1" />
                                <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                    <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                    <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                                </chartingprimitives:EdgePanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
    

    This details the location of the plot area, title, legend etc... It also included a hard-coded margin around the plot area, so you cannot achieve what you are after by simply styling the chart. If you just want a chart area and nothing else, you can simplify the chart template as follows:

    xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    
    <Grid>
      <chartingToolkit:Chart x:Name="chart" Width="500" Height="300"
                             Margin="0, 0, 0, 0" Padding="0">
        <chartingToolkit:AreaSeries ItemsSource="{Binding}"  
                                      DependentValuePath="Value"
                                      IndependentValuePath="Key"
                                      Background="Red">
        </chartingToolkit:AreaSeries>
        <chartingToolkit:Chart.Axes>
          <chartingToolkit:LinearAxis Orientation="X" ShowGridLines="False" Height="0">
          </chartingToolkit:LinearAxis>
          <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="False" Width="0"/>
        </chartingToolkit:Chart.Axes>
        <chartingToolkit:Chart.Template>
          <ControlTemplate TargetType="chartingToolkit:Chart">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}">
              <Grid>
                <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                  <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                  <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                </chartingprimitives:EdgePanel>
              </Grid>
            </Border>
          </ControlTemplate>
        </chartingToolkit:Chart.Template>
      </chartingToolkit:Chart>
    </Grid>
    

    This will remove the padding that you are seeing.

    0 讨论(0)
提交回复
热议问题