【问题标题】:How to resize dots on WPF chart?如何调整 WPF 图表上的点大小?
【发布时间】:2016-12-14 21:37:18
【问题描述】:

我正在使用图表工具。这是我的 wpf 代码:

<Window x:Class="UserGraphShow.GraphOutput"
    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:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    mc:Ignorable="d"

    Title="MainWindow" Height="446" Width="726" >

    <Grid>
        <DVC:Chart Name="Chart"  Grid.ColumnSpan="3" Background="Blue" Title="Line">
            <DVC:Chart.Series>
                <DVC:LineSeries Title=" Your Graph"  IndependentValueBinding="{Binding Path=Key}"  DependentValueBinding="{Binding Path=Value}" Opacity="0" />
            </DVC:Chart.Series>

            <DVC:Chart.DataContext >
                <Style TargetType="Grid" >
                    <Setter Property="Opacity" Value="0" />
                </Style>
            </DVC:Chart.DataContext>
            <DVC:Chart.Axes>
                <DVC:LinearAxis Orientation="Y" Minimum="-302" Maximum="0"/>
                <DVC:LinearAxis Orientation="X" Maximum="509" Minimum="0"/>
                <DVC:LinearAxis Visibility="Hidden"/>
            </DVC:Chart.Axes>
        </DVC:Chart>
        <Button x:Name="Button" Content="Show" HorizontalAlignment="Left" Margin="8,10,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="22" Click="button_Click"/>
    </Grid>
</Window>

我正在尝试将 x[] 和 y[] 的数组显示为绘图。 这是一个按钮的代码:

private void button_Click(object sender, RoutedEventArgs e)
{
    var b = GetUserGraphUnfoInfo.FindXy("../../../Main_Logic/image.jpeg");
    var x = b[0];  // array of x
    var y = b[1]; // array of y
    var ls = new LineSeries
    {
        IndependentValueBinding = new Binding("Key"),
        DependentValueBinding = new Binding("Value")
    };

    var a = new KeyValuePair<int, int>[x.Length-1];
    for (var i = 0; i < x.Length-1; i++)
        a[i] = new KeyValuePair<int, int>(x[i], y[i]);

    ls.ItemsSource = a;
    Chart.Series.Clear();
    Chart.Series.Add(ls);        
}

一切正常,虽然点太大了,我该如何去除它们?

这是我得到的:

【问题讨论】:

    标签: c# wpf xaml charts wpftoolkit


    【解决方案1】:

    为您的数据点创建和应用样式,如下所示:

    XAML:

    <Window x:Class="WpfApplication342.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:dvc="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        xmlns:local="clr-namespace:WpfApplication342"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.DataContext>
        <PointCollection>1,10 2,20, 3,30</PointCollection>
    </Window.DataContext>
    
    <Window.Resources>
    
        <Style x:Key="LineDataPointStyle1" TargetType="{x:Type dvc:LineDataPoint}">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="BorderBrush" Value="Gray"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Width" Value="64"/>
            <Setter Property="Height" Value="64"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type dvc:LineDataPoint}">
                        <Grid x:Name="Root" Opacity="1">
                            <Grid.ToolTip>
                                <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                            </Grid.ToolTip>
                            <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}"/>
                            <Ellipse RenderTransformOrigin="0.661,0.321">
                                <Ellipse.Fill>
                                    <RadialGradientBrush GradientOrigin="0.681,0.308">
                                        <GradientStop Color="Transparent"/>
                                        <GradientStop Color="#FF3D3A3A" Offset="1"/>
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Ellipse x:Name="SelectionHighlight" Fill="Red" Opacity="0"/>
                            <Ellipse x:Name="MouseOverHighlight" Fill="White" Opacity="0"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    </Window.Resources>
    
    <Grid>
        <dvc:Chart>
            <dvc:LineSeries ItemsSource="{Binding}" 
                            DependentValuePath="Y"
                            IndependentValuePath="X" 
                            DataPointStyle="{DynamicResource LineDataPointStyle1}"/>
        </dvc:Chart>
    </Grid>
    

    现在您可以通过多种方式操作样式以消除圆点:使用透明颜色、将宽度和高度设置为零,甚至完全修改控件模板。将宽度和高度设置为零:

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多