【发布时间】:2012-02-03 06:34:22
【问题描述】:
我需要使用 LineSeries 自定义标准 Flex LineChart 的外观。当鼠标指针悬停在数据点上时,我无法弄清楚如何更改默认绘制的圆圈。
【问题讨论】:
标签: apache-flex charts customization
我需要使用 LineSeries 自定义标准 Flex LineChart 的外观。当鼠标指针悬停在数据点上时,我无法弄清楚如何更改默认绘制的圆圈。
【问题讨论】:
标签: apache-flex charts customization
如果要删除这些圈子,请将图表的属性showDataTipTargets 设置为false。如果你想自定义它们,你可以:
dataTipRenderer 并在那里执行蒙皮和绘图。LineChart 并覆盖positionAllDataTips 方法,该方法在ChartBase 类中定义。下面是代码,负责绘制圆圈:代码:
if (showTarget) {
if (len>1) {
if (calloutStroke) {
calloutStroke.apply(g, null, null);
if (tipData.isRight) {
g.moveTo(localPts.x,
localPts.y+tipData.height/2);
g.lineTo(tipData.x,
localPts.y+tipData.height/2);
g.lineTo(tipData.x, tipData.y);
}
else {
if (layoutDirection == LayoutDirection.RTL) {
g.moveTo(localPts.x-tipData.width,
localPts.y+tipData.height/2);
}
else {
g.moveTo(localPts.x+tipData.width,
localPts.y+tipData.height/2);
}
g.lineTo(tipData.x,
localPts.y+tipData.height/2);
g.lineTo(tipData.x, tipData.y);
}
}
}
var tipColor:uint=tipData.hd.contextColor;
g.lineStyle(1, tipColor, 100);
g.moveTo(tipData.x, tipData.y);
g.beginFill(0xFFFFFF, 1);
g.drawCircle(tipData.x, tipData.y, TOOLTIP_TARGET_RADIUS);
g.endFill();
【讨论】:
Timofei Davydik 给出了一个非常好的答案,它也帮助我处理数据提示
但是,我在 cmets 中看到,对于如何准确地覆盖 ChartBase.positionDataTips 和 ChartBase.positionAllDataTips 这两个大型函数存在一些困惑。
一种方法是执行以下操作:
showDataTipTargets 设置为 false。showCustomDataTipTargets。positionDataTips 和positionAllDatatips
新的覆盖函数应该如下所示:
override protected function positionDataTips():void
{
this.setStyle("showDataTipTargets", false);
// this will do all the normal rendering of the datatips and callout
// but it will not draw the dataTipTarget because that is dependent upon
// the style, showDataTipTargets
super.positionDataTips();
// now here you draw your custom datatip target.
// Use the following code as a guide, it is
// copied from the ChartBase.positionDataTips
/*
if (len > 1)
{
if (calloutStroke)
{
calloutStroke.apply(g,null,null);
if (tipData.isRight)
{
g.moveTo(chartLocalPts.x,
chartLocalPts.y + tipData.height / 2);
g.lineTo(tipData.x,
chartLocalPts.y + tipData.height / 2);
g.lineTo(tipData.x, tipData.y);
}
else
{
if(layoutDirection == LayoutDirection.RTL)
{
g.moveTo(chartLocalPts.x - tipData.width,
chartLocalPts.y + tipData.height / 2);
}
else
{
g.moveTo(chartLocalPts.x + tipData.width,
chartLocalPts.y + tipData.height / 2);
}
g.lineTo(tipData.x,
chartLocalPts.y + tipData.height / 2);
g.lineTo(tipData.x, tipData.y);
}
}
}
var tipColor:uint = tipData.hd.contextColor;
g.lineStyle(1, tipColor, 100);
g.moveTo(tipData.x, tipData.y);
g.beginFill(0xFFFFFF, 1);
g.drawCircle(tipData.x, tipData.y, TOOLTIP_TARGET_RADIUS);
g.endFill();
g.beginFill(tipColor, 1);
g.drawCircle(tipData.x, tipData.y,
TOOLTIP_TARGET_INNER_RADIUS);
g.endFill();
*/
}
【讨论】:
解决方案
有必要创建一个自定义itemRenderer 并在MOUSE_OVER 事件处理程序中绘制您想要的任何内容。图表的showDataTipTargets 属性必须设置为false
在寻找解决方案时,我忘记了itemRenderer 是 Flex 组件并且可以处理鼠标事件。我的同事指出了它并帮助解决了这个问题。
代码
CustomDataTipTargetRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="init()"
...>
<fx:Script>
<![CDATA[
private function init():void
{
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
private function onMouseOver(event:MouseEvent):void
{
// Show custom data tip target point.
}
private function onMouseOut(event:MouseEvent):void
{
// Hide custom data tip target point.
}
]]>
</fx:Script>
</s:ItemRenderer>
YourView.mxml
<mx:LineChart ...
showDataTips="true"
showDataTipTargets="false">
...
<mx:series>
<mx:LineSeries ... itemRenderer="yournamespace.CustomDataTipTargetRenderer">
...
</mx:LineSeries>
</mx:series>
</mx:LineChart>
【讨论】: