【问题标题】:WPF chart toolkit axis scale with custom value具有自定义值的 WPF 图表工具包轴刻度
【发布时间】:2012-11-27 08:14:59
【问题描述】:

目前我正在尝试使用 WPF 图表工具包制作图表,
要求 Y 轴应如下图所示:

描述
*0~84 为 D 级
*85~99 为 B 级
...

我不确定图表工具包是否可以制作这种类型的 Y 轴刻度。
如果图表工具包不支持以上内容,我将尝试将 TextBox 控件直接放入画布中以获取“Level *”标签,但我仍然有问题如何仅将 85、100、115 显示为 Y 轴的比例。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: wpf charts


    【解决方案1】:

    您可以尝试使用转换器作为图表的轴标签。像这样的东西: XAML

    <chartingToolkit:Chart Name="chart1"  >
     <chartingToolkit:Chart.Resources>
      <HideConverter x:Key="HideConverter1" />
     </chartingToolkit:Chart.Resources>
     <chartingToolkit:Chart.Axes>
      <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="False"  Interval="5" Minimum="0" Maximum="150" >
       <chartingToolkit:LinearAxis.AxisLabelStyle>
        <Style TargetType="chartingToolkit:AxisLabel">
         <Setter Property="Template">
          <Setter.Value>
           <ControlTemplate TargetType="chartingToolkit:AxisLabel">
            <TextBlock DataContext="{TemplateBinding FormattedContent}" Text="{Binding Converter={StaticResource HideConverter1}}" />
           </ControlTemplate>
          </Setter.Value>
         </Setter>
        </Style>
       </chartingToolkit:LinearAxis.AxisLabelStyle>
      </chartingToolkit:LinearAxis>
     </chartingToolkit:Chart.Axes>
    </chartingToolkit:Chart>
    

    在您的代码隐藏中,转换器是

    public class HideConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // value is the current tick on the Y axis
            int x = int.Parse(value.ToString());
            switch (x)
            {
                case 85:
                case 100:
                case 115:
                    return value;
                case 40: // I set 40 to be in the middle between 0 and 85
                    return "Level D";
                case 90:
                    return "Level C";
                case 110:
                    return "Level B";
                case 130:
                    return "Level A";
                default:
                    return null;;
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    HTH

    【讨论】:

    • 感谢您的建议。我刚刚隐藏了整个 Y 轴(可见性 =“隐藏”)并在父画布中重叠了一个图像。这种情况下不需要的“刻度”不会显示在轴中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多