首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎样才能画出像HTC 'Stocks‘这样漂亮的图表呢

我怎样才能画出像HTC 'Stocks‘这样漂亮的图表呢
EN

Stack Overflow用户
提问于 2011-02-10 21:11:02
回答 2查看 3.8K关注 0票数 5

通过我的HTC Mozart,我下载了一款名为“股票”的应用程序,它基本上可以管理股票(纳斯达克等)。

这些图表/图表看起来很不错,我想知道我如何才能做类似的事情。这是一张图表的图片:

我看到三种颜色:-曲线上方(背景)-曲线本身-曲线下方

有没有免费/开放的资源可以帮我画这样的图?欢迎任何博客帖子或链接!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-10 21:23:16

您可以使用Silverlight工具包中的图表控件,如this blog post中所述。

Visifire还提供了chart control

票数 3
EN

Stack Overflow用户

发布于 2011-02-16 05:13:26

基本上,如果您选择自己编写代码来完成此操作,则需要创建三个图形元素:

  1. 背景,例如深蓝色区域,这可能只是画布或网格本身
  2. 火花线(你所说的曲线),这是一堆线段建立在一个点集合
  3. 多边形,其中包含一组与火花线相同的点,加上从最后一个X2绘制的线段,Y2坐标到图形的底部,另一个线段从右下角回到左下角,以及从左下角到第一个X1,Y1坐标的最后一个线段。这个多边形将被添加到画布上的火花线之前,并将被赋予不同于背景的填充。

使用一些示例代码更新此代码。假设您有一个原始的Y值列表(我很抱歉没有时间发布一个更完整的示例):

代码语言:javascript
运行
复制
// Declare a Polyline for the spark line
Polyline sparkLine = new Polyline();

// Get style from the XAML user control resources
sparkLine.Style = (Style)TryFindResource("graphLine");

// Declare a polygon for the background
Polygon backgroundPolygon = new Polygon();

// Get its style
backgroundPolygon.Style = (Style)TryFindResource("graphBackground");

// PointCollection for the graph
PointCollection graphPointsCollection = new PointCollection();

// The X value for each point just gets advanced by a uniform amount for each
// Y value on the graph, in this case by an int called gridUnit, which was defined elsewhere
int currentX = 0;

// Get the range covering the min and max graph bounds
decimal graphValRange = maxGraphVal - minGraphVal;

// Traverse the numeric values in a list, create points and add them to the PointCollection
foreach (decimal graphVal in graphValues)
{
    // Calculate the Y2 value as a percentage
    decimal graphY2Val = (graphVal - minGraphVal) / graphValRange;

    // Then apply that percentage to the overall graph height and that will be our Y2 value.
    // NOTE: Since Y values are inverse, we subtract it from the graph height to render it correctly
    double graphY2ValDouble = Convert.ToDouble(graphHeight - (graphY2Val * graphHeight));

    // Create a point from the X and Y values
    Point currentPoint = new Point(currentX, graphY2ValDouble);

    // Add it to the collection
    graphPointsCollection.Add(currentPoint);

    // Advance the X value each time (as a multiple of the grid unit)
    currentX += gridUnit;
}

// For the background we'll use all the same points but need to clone. Otherwise,
// when some additional points are added they would also end up in the spark line
PointCollection backgroundPointsCollection = graphPointsCollection.Clone();

// Now add additional points to collection to create background polygon.
// These will allow the polygon to be drawn to bottom right
// and back to bottom left, completing the polygon.
Point bottomRightPoint = new Point(currentX - gridUnit, graphHeight);
Point bottomLeftPoint = new Point(0, graphHeight);

backgroundPointsCollection.Add(bottomRightPoint);
backgroundPointsCollection.Add(bottomLeftPoint);

// Now assign the points to the background polygon
backgroundPolygon.Points = backgroundPointsCollection;

// Add the background to the graph first so it doesn't cover the spark line
Graph.Children.Add(backgroundPolygon);

// Finally, assign the graph points to the spark line
sparkLine.Points = graphPointsCollection;

// Add the spark line to the graph
Graph.Children.Add(sparkLine);

以下是样式的XAML:

代码语言:javascript
运行
复制
<Style TargetType="Polyline" x:Key="graphLine">
    <Setter Property="Stroke" Value="#96EF4223" />
    <Setter Property="StrokeThickness" Value="2" />
</Style>

<Style TargetType="Polygon" x:Key="graphBackground">
    <Setter Property="Fill">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,0.003" StartPoint="0.5,0.994">
                <GradientStop Color="#24FAF8CA" Offset="0"/>
                <GradientStop Color="#246EBDE9" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

我使用这样的代码来生成这样的图形:

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4957485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档