Flex :给LineChart设置坐标轴最大最小范围

本文介绍了一个使用Adobe Flex创建的图表应用程序实例。该程序利用LineChart组件展示了一段时间内的股票价格变动情况,并通过调整滑块来改变图表显示的价格区间。此外,还实现了货币格式化的y轴标签显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


复制代码<?xml version="1.0"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
        layout="vertical" 
        verticalAlign="middle" 
        backgroundColor="white" 
        preinitialize="init();"> 
  
    <mx:Script> 
        <![CDATA[ 
            import mx.charts.chartClasses.IAxis; 
            import mx.formatters.CurrencyFormatter; 
  
            private var currFormatter:CurrencyFormatter; 
  
            private function init():void { 
                currFormatter = new CurrencyFormatter(); 
                currFormatter.precision = 2; 
            } 
  
            private function linearAxis_labelFunc(item:Object, prevValue:Object, axis:IAxis):String { 
                return currFormatter.format(item); 
            } 
  
            private function lineChart_creationComplete():void { 
                var linearAxisMinimum:int = Math.floor(linearAxis.computedMinimum); 
                var linearAxisMaximum:int = Math.ceil(linearAxis.computedMaximum); 
                linearAxis.minimum = linearAxisMinimum; 
                linearAxis.maximum = linearAxisMaximum; 
            } 
        ]]> 
    </mx:Script> 
  
    <mx:XMLListCollection id="dp"> 
        <mx:source> 
            <mx:XMLList> 
                <quote date="8/27/2007" open="40.38" close="40.81" /> 
                <quote date="8/24/2007" open="40.5" close="40.41" /> 
                <quote date="8/23/2007" open="40.82" close="40.6" /> 
                <quote date="8/22/2007" open="40.4" close="40.77" /> 
                <quote date="8/21/2007" open="40.41" close="40.13" /> 
                <quote date="8/20/2007" open="40.55" close="40.74" /> 
                <quote date="8/17/2007" open="40.18" close="40.32" /> 
                <quote date="8/16/2007" open="39.83" close="39.96" /> 
                <quote date="8/15/2007" open="40.22" close="40.18" /> 
                <quote date="8/14/2007" open="41.01" close="40.41" /> 
                <quote date="8/13/2007" open="41" close="40.83" /> 
                <quote date="8/10/2007" open="41.3" close="41.06" /> 
                <quote date="8/9/2007" open="39.9" close="40.75" /> 
                <quote date="8/8/2007" open="39.61" close="40.23" /> 
                <quote date="8/7/2007" open="39.08" close="39.42" /> 
                <quote date="8/6/2007" open="38.71" close="39.38" /> 
                <quote date="8/3/2007" open="39.47" close="38.75" /> 
                <quote date="8/2/2007" open="39.4" close="39.52" /> 
                <quote date="8/1/2007" open="40.29" close="39.58" /> 
            </mx:XMLList> 
        </mx:source> 
    </mx:XMLListCollection> 
  
    <mx:ApplicationControlBar dock="true"> 
        <mx:Form styleName="plain"> 
            <mx:FormItem label="minimum:"> 
                <mx:HSlider id="minSlider" 
                        minimum="28" 
                        maximum="38" 
                        value="38" 
                        liveDragging="true" 
                        snapInterval="1" 
                        change="linearAxis.minimum = event.value;" /> 
            </mx:FormItem> 
            <mx:FormItem label="maximum:"> 
                <mx:HSlider id="maxSlider" 
                        minimum="42" 
                        maximum="52" 
                        value="42" 
                        liveDragging="true" 
                        snapInterval="1" 
                        change="linearAxis.maximum = event.value;" /> 
            </mx:FormItem> 
        </mx:Form> 
  
        <mx:Spacer width="100%" /> 
  
        <mx:Legend dataProvider="{lineChart}" 
                direction="horizontal" /> 
    </mx:ApplicationControlBar> 
  
    <mx:LineChart id="lineChart" 
            showDataTips="true" 
            dataProvider="{dp}" 
            width="100%" 
            height="100%" 
            creationComplete="lineChart_creationComplete();"> 
  
        <mx:backgroundElements> 
            <mx:GridLines id="gridLines" 
                    direction="both" 
                    verticalTickAligned="false"> 
                <mx:verticalStroke> 
                    <mx:Stroke color="haloSilver" 
                            weight="0" 
                            alpha="1.0" /> 
                </mx:verticalStroke> 
                <mx:horizontalStroke> 
                    <!-- Set alpha to 0 so stroke isn't visible. --> 
                    <mx:Stroke color="white" 
                            weight="0" 
                            alpha="0.0" /> 
                </mx:horizontalStroke> 
                <mx:horizontalFill> 
                    <mx:SolidColor color="haloSilver" 
                            alpha="0.1" /> 
                </mx:horizontalFill> 
            </mx:GridLines> 
        </mx:backgroundElements> 
  
        <!-- vertical axis ** minorInterval:y轴线条的间隔  interval:y轴数值的间隔,maximum:y轴最大值 , minimum:y轴最小值 --> 
        <mx:verticalAxis> 
            <mx:LinearAxis id="linearAxis" 
                    baseAtZero="false" 
                    title="Price (USD)" 
                    minorInterval="0.10" 
                    interval="0.5" 
                     maximum="78" minimum="0"
                    labelFunction="linearAxis_labelFunc" /> 
        </mx:verticalAxis> 
  
        <!-- horizontal axis --> 
        <mx:horizontalAxis> 
            <mx:CategoryAxis id="ca" 
                    categoryField="@date" 
                    title="Date" /> 
        </mx:horizontalAxis> 
  
        <!-- horizontal axis renderer --> 
        <mx:horizontalAxisRenderers> 
            <mx:AxisRenderer axis="{ca}" 
                    canDropLabels="true" /> 
        </mx:horizontalAxisRenderers> 
  
        <!-- series --> 
        <mx:series> 
            <mx:LineSeries yField="@open" 
                    displayName="Open" /> 
        </mx:series> 
    </mx:LineChart> 
  
</mx:Application>
复制代码


function initChart() { let series = []; let yAxis = []; let timeList = []; if (propertyValues.value) { Object.keys(propertyValues.value).forEach((key, index) => { let timeArr = propertyValues.value[key].map(item => new Date(item.localDateTime).getTime() ); timeList = timeList.length > timeArr.length ? timeList : timeArr; series.push({ name: findPropertyName(key), showSymbol: false, type: "line", data: propertyValues.value[key].map(item => [ item.localDateTime, item.value ]) }); }); yAxis.push({ name: transformI18n($t("IOT.purePropertyValue")), type: "value", axisLine: { show: true, symbol: ["none", "arrow"], // 显示线的起始箭头,不显示结束箭头 lineStyle: { color: "rgba(117,127,158,0.8)" } }, axisTick: { show: false } }); } if (boolCollection.value) { Object.keys(boolCollection.value).forEach(key => { let timeArr = boolCollection.value[key].map(item => new Date(item.localDateTime).getTime() ); timeList = timeList.length > timeArr.length ? timeList : timeArr; series.push({ name: findPropertyName(key), yAxisIndex: yAxis.length, type: "line", showSymbol: false, step: "middle", data: boolCollection.value[key].map(item => [ item.localDateTime, item.value ]) }); }); yAxis.push({ name: transformI18n($t("IOT.pureBoolean")), type: "value", min: 1, max: 0, interval: 1, // 强制间隔为1 axisLine: { show: true, symbol: ["none", "arrow"], // 显示线的起始箭头,不显示结束箭头 lineStyle: { color: "rgba(117,127,158,0.8)" } }, axisTick: { show: false }, axisLabel: { formatter: val => (val ? "true" : "false") } }); } const minTime = Math.min(...timeList); const maxTime = Math.max(...timeList); const timeRange = maxTime - minTime; // 设置固定刻度数 const interval = timeRange / 5; // 6 个点需要 5 个间隔 setOptions({ title: { text: props.displayName }, tooltip: { trigger: "axis", formatter: function (params) { let result = params[0].axisValueLabel + "<br/>"; params.forEach(function (item) { result += ` <div style=" display:flex; align-items:center; margin:3px 0; "> <span style=" display:inline-block; width:10px; height:10px; border-radius:50%; background:${item.color || "#666"}; margin-right:8px; "></span> <span style="flex:1;">${item.seriesName}</span> <span style="font-weight:bold;margin-left:10px;">${item.value[1]}</span> </div>`; }); return result; } }, legend: { show: true }, dataZoom: [ { type: "slider", show: true, left: "10%", right: "10%", bottom: "2%", start: 0, end: 100, backgroundColor: "#fff", borderColor: "#D9E0EF", fillerColor: "rgba(158,183,244,0.4)", dataBackground: { areaStyle: { color: "#F6F8FC" } }, handleStyle: { color: "#fff", borderColor: "#D9E0EF", borderWidth: 1, borderCap: "butt" } // "linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)", }, { type: "inside" } ], grid: { left: "3%", right: "4%", bottom: "15%", containLabel: true }, color: "#409eff", xAxis: { name: "时间", type: "time", boundaryGap: false, min: minTime, max: maxTime, interval: interval, axisLabel: { show: true, formatter: function (value, index) { let time = dayjs(value).format("YYYY-MM-DD HH:mm:ss"); return time; } }, axisLine: { lineStyle: { color: "rgba(117,127,158,0.8)" }, symbol: ["none", "arrow"] }, nameGap: yAxis.length > 1 ? 40 : 15 } as any, yAxis: yAxis.length > 1 ? yAxis : yAxis[0], series: series }); window.addEventListener("resize", () => { resize(); }); }x轴设置固定刻度数无效
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值