我认为没有标准的 Flex API 可以解决这个问题。您可以通过控制用户与数据点的交互来以另一种方式实现它。
这是我的解决方案和工作示例。
用户可以获取任何数据点并上下拖动。可以使用您拥有的任何系列的 localToDate 方法通过转换鼠标坐标来计算新位置。
重要的一点是要考虑图表位置和绘图实际开始之间的距离。为此,我使用常量 CHART_GUTTERTOP。
编码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.charts.events.ChartItemEvent;
import mx.charts.series.items.LineSeriesItem;
import mx.collections.ArrayCollection;
[Bindable]public var expenses:ArrayCollection = new ArrayCollection([
{Month:"Jan", Profit:2000, Expenses:1500, Amount:450},
{Month:"Feb", Profit:1000, Expenses:200, Amount:600},
{Month:"Mar", Profit:2200, Expenses:800, Amount:300},
{Month:"Apr", Profit:1800, Expenses:400, Amount:400}
]);
private var currentId:int = -1;
private var currentYField:String;
private const CHART_GUTTERTOP:int = 20;
protected function myChart_itemMouseDownHandler(event:ChartItemEvent):void
{
if (event.hitData != null)
{
var chartItem:LineSeriesItem = event.hitData.chartItem as LineSeriesItem;
currentId = chartItem.index;
currentYField = (chartItem.element as LineSeries).yField;
}
}
protected function myChart_itemMouseUpHandler(event:ChartItemEvent):void
{
if (currentId != -1)
{
var p:Point = new Point(myChart.mouseX, myChart.mouseY - CHART_GUTTERTOP);
var d:Array = series1.localToData(p);
expenses.getItemAt(currentId)[currentYField] = d[1];
myChart.dataProvider = expenses;
currentId = -1;
}
}
]]>
</fx:Script>
<s:Panel x="20" y="20">
<s:VGroup>
<mx:LineChart
id="myChart"
gutterTop="{CHART_GUTTERTOP}"
dataProvider="{expenses}"
showDataTips="true"
itemMouseDown="myChart_itemMouseDownHandler(event)"
itemMouseUp="myChart_itemMouseUpHandler(event)">
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{expenses}"
categoryField="Month"/>
</mx:horizontalAxis>
<mx:series>
<mx:LineSeries id="series1"
yField="Profit"
displayName="Profit"/>
<mx:LineSeries
yField="Expenses"
displayName="Expenses"/>
</mx:series>
</mx:LineChart>
<mx:Legend dataProvider="{myChart}"/>
</s:VGroup>
</s:Panel>
</s:Application>