Extjs 4 chart自定义坐标轴刻度
Sencha出品的ExtJs是一个非常优秀的前端框架,尤其是具有里程碑意义的4.0的发布。4.0采用MVC架构和全新的class系统,并且提供了非常丰富的组件。但是,尽管ExtJS如此强大,仍有不尽人意的地方。比如,chart里坐标轴的刻度通常是均匀分布的,ExtJS的实现也是通过坐标轴的最大值和最小值以及其他参数配置均匀的计算刻度。但是,在工作过程中碰到需要自定义刻度的情况,如下图所示

水平轴的刻度是5,10,20这样的不均匀值,但是ExtJS不支持这样的功能(至少我翻遍了文档也没找到)。最初想到的办法是隐藏不需要的label,可这样写:
{
type: 'Numeric',
position: 'bottom',
fields: ['x'],
title: xAxisTitle,
minimum: xRange.xMinimum,
maximum: xRange.xMaximum,
majorTickSteps: 5,
minorTickSteps: 10,
label: {
renderer: function(val) {
//lables: [5, 10, 20]
if (labels.indexOf(val) < 0) {
return '';
}
return val;
}
},
grid: true
}
但是这样写有两个问题,一是需要显示的label值不一定会出现在renderer中,二是即便label没显示,垂直的网格线(通过配置代码中红色部分的属性可以调节网格线密度)还是会出现,这可能并不是我们想要的。通过查阅源码发现,在坐标轴内部实现中,是通过坐标轴范围和刻度个数来计算的,并且网格线跟坐标刻度是一致的。在源码文件ext\src\chart\axis\Axis.js 480行:
// Build the array of steps out of the fixed-value 'step'.
steps = new Array;
for (val = +me.from; val < +me.to; val += step) {
steps.push(val);
}
steps.push(+me.to);
之后渲染label和网格线都用到steps这个数组。所以,可以在这个地方做点手脚,把steps变量强行改成我们需要的数组,让ExtJS按照我们的意图去做。显然,不能直接去修改Ext的源码文件。这里有两种办法,一是用Ext.override方法去重写Ext.chart.axis.Axis的drawAxis方法,二是在Axis的配置参数里提供该方法的自定义实现。前者会影响到之后的所有用到坐标轴的chart,后者只会影响当前的chart. 代码改动如下:
Ext.syncRequire('Ext.chart.axis.Axis');
Ext.override(Ext.chart.axis.Axis, {
/**
* Renders the axis into the screen and updates its position.
*/
drawAxis: function(init) {
var me = this,
i,
x = me.x,
y = me.y,
dashSize = me.dashSize,
length = me.length,
position = me.position,
verticalAxis = (position == 'left' || position == 'right'),
inflections = [],
calcLabels = (me.isNumericAxis),
stepCalcs = me.applyData(),
step = stepCalcs.step,
steps = stepCalcs.steps,
stepsArray = Ext.isArray(steps),
from = stepCalcs.from,
to = stepCalcs.to,
// If we have a single item, to - from will be 0.
axisRange = (to - from) || 1,
trueLength,
currentX,
currentY,
path,
subDashesX = me.minorTickSteps || 0,
subDashesY = me.minorTickSteps || 0,
dashesX = Math.max(subDashesX + 1, 0),
dashesY = Math.max(subDashesY + 1, 0),
dashDirection = (position == 'left' || position == 'top' ? -1 : 1),
dashLength = dashSize * dashDirection,
series = me.chart.series.items,
firstSeries = series[0],
gutters = firstSeries ? firstSeries.nullGutters : me.nullGutters,
padding,
subDashes,
subDashValue,
delta = 0,
stepCount = 0,
tick, axes, ln, val, begin, end;
me.from = from;
me.to = to;
// If there is nothing to show, then leave.
if (me.hidden || (from > to)) {
return;
}
// If no steps are specified (for instance if the store is empty), then leave.
if ((stepsArray && (steps.length == 0)) || (!stepsArray && isNaN(step))) {
return;
}
if (stepsArray) {
// Clean the array of steps:
// First remove the steps that are out of bounds.
steps = Ext.Array.filter(steps, function(elem, index, array) {
return (+elem > +me.from && +elem < +me.to);
}, this);
// Then add bounds on each side.
steps = Ext.Array.union([me.from], steps, [me.to]);
} else {
// Build the array of steps out of the fixed-value 'step'.
steps = new Array;
70 if (me.fixedSteps) {
71 steps = me.fixedSteps;
72 } else {
for (val = +me.from; val < +me.to; val += step) {
74 steps.push(val);
75 }
76 steps.push(+me.to);
77 }
}
...//此处省略其他原有代码
}
});
代码中红色部分if语句块里就是偷梁换柱的地方。使用的时候,在axis的配置代码里加上fixedSteps属性就行了。
{
type: 'Numeric',
position: 'bottom',
fields: ['x'],
title: xAxisTitle,
minimum: xRange.xMinimum,
maximum: xRange.xMaximum,
fixedSteps: [5, 10,20],
grid: true
}
至此,雕虫小技大功告成。欢迎拍砖。
Extjs 4 chart自定义坐标轴刻度的更多相关文章
- extjs 4 chart 时间轴格式的处理
var dayStore = Ext.create('Ext.data.JsonStore', { fields: [{ name: 'name', type: 'date', dateFormat: ...
- Android 自定义带刻度的seekbar
自定义带刻度的seekbar 1.布局 <span style="font-family:SimHei;font-size:18px;"><com.imibaby ...
- R语言修改标题、坐标轴刻度、坐标轴名称的大小(cex.axis、cex.lab、cex.main函数)
修改标题.坐标轴刻度.坐标轴名称的大小,用到了cex.axis.cex.lab.cex.main函数,其中,cex.axis表示修改坐标轴刻度字体大小,cex.lab表示修改坐标轴名称字体大小,cex ...
- Python之坐标轴刻度细化、坐标轴设置、标题图例添加
学习python中matplotlib绘图设置坐标轴刻度.文本 http://www.jb51.net/article/134638.htm Python绘图 https://www.cnblogs. ...
- 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...
- 63.ExtJs事件(自定义事件、on、eventManager)示例
转自:https://blog.csdn.net/leadergg/article/details/5927614?utm_source=blogxgwz5 ExtJs事件(自定义事件.on.even ...
- 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变
带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115 近期有 ...
- 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求
本文链接:https://blog.csdn.net/qq_26909801/article/details/96966372数值型坐标轴刻度计算算法前言算法描述上代码代码运行效果结语前言因实习的公司 ...
- [原创][开源] SunnyUI.Net 开发日志:UIBarChart 坐标轴刻度取值算法
_ 在开发UIBarChart的过程中,需要绘制Y轴的刻度,数据作图时,纵横坐标轴刻度范围及刻度值的取法,很大程度上取决于数据的分布.对某一组数据,我们很容易就能知道如何选取这些值才能使图画得漂亮.但 ...
随机推荐
- WebService中控制字符的处理
情景 最近项目中很多WebService都发不出去,报的错误如下: Invalid white space character in text to output (in xml 1.1 ...
- js 获取 当前时间 时间差 时间戳 倒计时
开发web一段时间后发现经常使用时间进行一些操作,比较多的就是获取当前时间.对时间进行比较.时间倒计时.时间戳这些部分,每次去用经常忘总是需要去查询,还是自己总结一下比较靠谱. 获取时间戳的方法: 第 ...
- 【leetcode】354. Russian Doll Envelopes
题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...
- 5中方式实现String反转
这里介绍Java中5中实现String反转的方式. 一.数组实现String反转 //数组实现String反转 public String reverseByArray(){ if(str == nu ...
- Emit
http://www.cnblogs.com/zhuweisky/archive/2008/09/20/1294666.html http://www.cnblogs.com/xiaoxiangfei ...
- Qt 日志宏
随便写了一个日志帮助的宏,既可以如同qDebug()一般在调试时输出信息,也可以在输出文本文件 #ifndef LOG_H #define LOG_H #include <QDir> #i ...
- 【原】隐藏ultraGrid1指定列
void uGrdAllFlight_InitializeRow(object sender, InitializeRowEventArgs e) { /***********TEST START** ...
- 好用的ssh工具oh-my-zsh / iterm2
The manual way 1. Clone the repository: git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh- ...
- PHP实现中文简体字和繁体字互转
function convert($str, $action='S'){ if($action != 'S' && $action != 'T'){ return $str; } $s ...
- SendMessage 窗口函数
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回. MSD ...