一个图表的例子

本文介绍了一个使用Silverlight实现的Excel页面设计示例,通过XAML定义了图表元素及交互逻辑,展示了如何动态加载和更新数据,同时实现了鼠标悬停时显示详细利润信息的功能。

<UserControl x:Class="SilverlightExcel.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="490" Height="298">
    <Grid x:Name="LayoutRoot" Background="White">
       
        <!-- Exported with Paste2Xaml and Ab2d.ReaderWmf. See http://www.wpf-graphics.com/ for more WPF tools. -->
        <Canvas>
            <Polygon Points="5,5 5,293 485,293 485,5 5,5" Fill="#FFFFFFFF"/>
            <Polygon Points="87,59 87,251 470,251 470,59 87,59" Fill="#FFFFFFFF"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M87,212L470,212 470,213 87,213 87,212z M87,174L470,174 470,175 87,175 87,174z M87,135L470,135 470,136 87,136 87,135z M87,97L470,97 470,98 87,98 87,97z M87,59L470,59 470,60 87,60 87,59"/>
            <Canvas Name="BarsCanvas">
                <Canvas.RenderTransform>
                    <ScaleTransform x:Name="BarsCanvasScale" ScaleY="0" CenterY="251"/>
                </Canvas.RenderTransform>
                <Canvas.Triggers>
                    <EventTrigger RoutedEvent="Canvas.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation From="0" To="1" Duration="0:0:1" Storyboard.TargetName="BarsCanvasScale" Storyboard.TargetProperty="ScaleY" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Canvas.Triggers>

                <Polygon Name="bar2003" Points="110,200 110,251 141,251 141,200 110,200" Fill="#FF4F81BD"/>
                <Polygon Name="bar2004" Points="187,190 187,251 218,251 218,190 187,190" Fill="#FFC0504D"/>
                <Polygon Name="bar2005" Points="263,186 263,251 294,251 294,186 263,186" Fill="#FF9BBB59"/>
                <Polygon Name="bar2006" Points="340,162 340,251 371,251 371,162 340,162" Fill="#FF8064A2"/>
                <Polygon Name="bar2007" Points="417,90 417,251 447,251 447,90 417,90" Fill="#FF4BACC6"/>
            </Canvas>
            <Polygon Points="87,250 87,59 88,59 88,250" Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M83,250L87,250 87,251 83,251 83,250z M83,212L87,212 87,213 83,213 83,212z M83,174L87,174 87,175 83,175 83,174z M83,135L87,135 87,136 83,136 83,135z M83,97L87,97 87,98 83,98 83,97z M83,59L87,59 87,60 83,60 83,59"/>
            <Polygon Points="87,250 470,250 470,251 87,251" Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M88,250L88,254 87,254 87,250 88,250z M165,250L165,254 164,254 164,250 165,250z M241,250L241,254 240,254 240,250 241,250z M318,250L318,254 317,254 317,250 318,250z M394,250L394,254 393,254 393,250 394,250z M471,250L471,254 470,254 470,250 471,250"/>
            <TextBlock Text="$0" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="61" Canvas.Top="243"/>
            <TextBlock Text="$50.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="28" Canvas.Top="205"/>
            <TextBlock Text="$100.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="166"/>
            <TextBlock Text="$150.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="128"/>
            <TextBlock Text="$200.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="90"/>
            <TextBlock Text="$250.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="51"/>
            <TextBlock Text="2003" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="111" Canvas.Top="263"/>
            <TextBlock Text="2004" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="188" Canvas.Top="263"/>
            <TextBlock Text="2005" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="265" Canvas.Top="263"/>
            <TextBlock Text="2006" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="341" Canvas.Top="263"/>
            <TextBlock Text="2007" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="418" Canvas.Top="263"/>
            <TextBlock Text="Net Profit" FontFamily="Arial" FontWeight="Bold" FontSize="24" Foreground="#FF000000" Canvas.Left="191" Canvas.Top="15"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="F1M5,5C5,5,5,5,5,5L485,5C485,5,486,5,486,5L486,293C486,293,485,294,485,294L5,294C5,294,5,293,5,293z M6,293L5,293 485,293 485,293 485,5 485,6 5,6 6,5z"/>

            <TextBlock Name="ValueTextBlock" Visibility="Collapsed" Text="$99.999,00" FontFamily="Arial" FontSize="13" FontWeight="Bold" Foreground="#FF000000"/>
            <!--<Grid Name="ValueCanvas" Visibility="Visible" Width="90" Height="25">
                <Rectangle RadiusX="3" RadiusY="3" Fill="#DDFFFFC0"/>
                <TextBlock Name="ValueTextBlock" Text="$999.999,00" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Arial" FontSize="13" FontWeight="Bold" Foreground="#FF000000"/>
            </Grid>-->
        </Canvas>
       
    </Grid>
</UserControl>

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightExcel
{
    public partial class Page : UserControl
    {
private const int START_YEAR = 2003;
private const int END_YEAR = 2007;

private Polygon _selectedPolygon;

private Dictionary<int, int> _yearProfits;

public Page()
{
    InitializeComponent();

    FillYearProfits();
    RegisterBarEvents();
}

private void FillYearProfits()
{
    _yearProfits = new Dictionary<int, int>();

    _yearProfits.Add(2003, 66000);
    _yearProfits.Add(2004, 79000);
    _yearProfits.Add(2005, 85000);
    _yearProfits.Add(2006, 116000);
    _yearProfits.Add(2007, 210000);
}

private int GetProfitForBar(string barName)
{
    int year;
    int profit;

    try
    {
        year = Int32.Parse(barName.Substring(3));

        profit = _yearProfits[year];
    }
    catch
    {
        profit = 0;
    }

    return profit;
}

private void RegisterBarEvents()
{
    for (int year = START_YEAR; year <= END_YEAR; year++)
    {
        Polygon oneBar = this.FindName("bar" + year.ToString()) as Polygon;

        if (oneBar != null)
        {
            oneBar.MouseEnter += new MouseEventHandler(oneBar_MouseEnter);
            oneBar.MouseLeave += new MouseEventHandler(oneBar_MouseLeave);
        }
    }
}

void oneBar_MouseEnter(object sender, MouseEventArgs e)
{
    Polygon currentBar = sender as Polygon;

    if (currentBar != null)
    {
        currentBar.StrokeThickness = 1;
        currentBar.Stroke = new SolidColorBrush(Colors.Black);

        _selectedPolygon = currentBar;

        ValueTextBlock.Text = string.Format("${0:#,000.00}",
                              GetProfitForBar(currentBar.Name));
        ValueTextBlock.Measure(new Size(double.PositiveInfinity,
                                        double.PositiveInfinity));

        Rect polygonBounds = GetPolygonBounds(currentBar.Points);

        ValueTextBlock.Visibility = Visibility.Visible;
       
        Canvas.SetLeft(ValueTextBlock,
            polygonBounds.X + (polygonBounds.Width / 2) -
            (ValueTextBlock.ActualWidth / 2));

        Canvas.SetTop(ValueTextBlock, polygonBounds.Y - 20);
    }
}

void oneBar_MouseLeave(object sender, MouseEventArgs e)
{
    if (_selectedPolygon != null)
    {
        _selectedPolygon.StrokeThickness = 0;
        _selectedPolygon.Stroke = null;

        ValueTextBlock.Visibility = Visibility.Collapsed;

        _selectedPolygon = null;
    }
}

private Rect GetPolygonBounds(PointCollection points)
{
    double minX, minY;
    double maxX, maxY;

    if (points == null || points.Count == 0) return Rect.Empty;

    maxX = 0;
    maxY = 0;
    minX = double.MaxValue;
    minY = double.MaxValue;

    foreach (Point onePoint in points)
    {
        if (onePoint.X < minX)
            minX = onePoint.X;

        if (onePoint.Y < minY)
            minY = onePoint.Y;

        if (onePoint.X > maxX)
            maxX = onePoint.X;

        if (onePoint.Y > maxY)
            maxY = onePoint.Y;
    }

    return new Rect(minX, minY, maxX - minX, maxY - minY);
}
    }
}

<think>好的,我现在需要回答用户关于Chat2DB生成可视化图表示例的问题。首先,用户提到参考了引用[1],其中有一个Chat2Plot的使用示例。可能用户混淆了Chat2DB和Chat2Plot,但根据引用内容,正确的工具应该是Chat2Plot。所以,我需要先确认这一点,并基于引用中的代码示例进行说明。 用户需要看到数据展示的例子,可能需要展示如何生成不同类型的图表,比如柱状图、折线图、散点图等。根据引用中的示例,用户需要设置API密钥、读取数据、初始化chat2plot,然后通过自然语言指令生成图表。 接下来,我需要确保回答结构清晰,分步骤说明,并生成相关问题。同时,要遵循系统级指令,正确使用LaTeX格式,比如在提到数学公式时用$...$包裹。例如,如果需要解释数据中的某些统计量,可能需要使用$符号,如平均值$\mu$。 另外,需要检查是否有需要独立成段的公式,如果有的话,使用$$...$$格式。但在这个问题中,可能涉及数学公式的机会不多,但如果有的话需要正确应用。 然后,生成相关问题,考虑用户可能感兴趣的方面,如支持的图表类型、如何自定义图表、处理大数据集的方法,以及与其他工具的比较。 最后,确保回答中的引用正确添加,引用标识放在相关段落末尾,如[^1]。同时,代码块需要正确格式化,使用三个反引号包裹,并标明语言类型。 总结步骤: 1. 指出用户可能将Chat2DB和Chat2Plot混淆,并基于引用中的Chat2Plot示例进行说明。 2. 分步说明如何使用Chat2Plot生成可视化图表,包括安装、设置API、数据读取、生成图表等。 3. 提供多种图表类型的示例代码,如柱状图、折线图、散点图。 4. 强调自然语言指令的灵活性,并给出复杂查询的例子。 5. 生成相关问题,覆盖用户可能关心的其他方面。 6. 确保LaTeX格式正确,引用标识正确放置,代码块正确格式化。</think>根据引用中的 Chat2Plot 示例,以下是生成可视化图表的完整数据展示流程和案例: ### 1. 基础柱状图示例 ```python import pandas as pd from chat2plot import chat2plot # 示例数据集(国家与GDP) data = { "Country": ["USA", "China", "Japan", "Germany", "India"], "GDP(trillion)": [26.95, 17.96, 4.23, 4.43, 3.39] } df = pd.DataFrame(data) # 生成柱状图 c2p = chat2plot(df) result = c2p("用柱状图显示各国GDP,按从高到低排序,美国用蓝色,中国用红色") result.figure.show() ``` 输出图表将显示按 GDP 降序排列的柱状图,其中美国的柱体为蓝色,中国为红色,其他默认颜色。 ### 2. 折线图与趋势分析 ```python # 时间序列数据示例(月度销售额) dates = pd.date_range(start="2023-01", periods=12, freq="M") sales = [120, 135, 98, 150, 210, 195, 230, 245, 190, 200, 180, 220] df = pd.DataFrame({"Date": dates, "Sales": sales}) # 生成带趋势线的折线图 result = c2p("绘制月销售额折线图,添加线性趋势线,标注最高点数值") result.figure.show() ``` 该图表将显示销售额波动情况,并带有趋势线和峰值标注,趋势线方程可能显示为 $y = ax + b$ 格式。 ### 3. 散点图与相关性分析 ```python # 生成身高体重数据集 import numpy as np np.random.seed(42) heights = np.random.normal(170, 10, 100) weights = 0.7*heights + np.random.normal(0, 5, 100) df = pd.DataFrame({"Height": heights, "Weight": weights}) # 生成带回归线的散点图 result = c2p("绘制身高体重散点图,添加回归线和相关系数,坐标轴单位厘米/千克") result.figure.show() ``` 图表将展示身高体重分布,回归线方程为 $y = 0.7x + \epsilon$,相关系数 $r^2$ 显示在图表角落。 ### 4. 组合图表示例 ```python # 生成多维度数据集 data = { "Product": ["A", "B", "C"]*4, "Quarter": ["Q1"]*3 + ["Q2"]*3 + ["Q3"]*3 + ["Q4"]*3, "Sales": [120,95,80,135,110,92,115,105,88,140,125,98] } df = pd.DataFrame(data) # 生成分组柱状图 result = c2p("按季度绘制各产品销售额的分组柱状图,添加折线显示季度总销售额趋势") result.figure.show() ``` 此组合图表将同时显示分组柱状图和叠加的折线趋势图,季度总销售额计算为 $\sum_{i=1}^{3} Sales_i$。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值