123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755 |
- topSuite("Ext.chart.AbstractChart", ['Ext.chart.*', 'Ext.data.ArrayStore'], function() {
- var chart, store;
- var Model = Ext.define(null, {
- extend: 'Ext.data.Model',
- fields: ['label', 'value']
- });
- function makeStore(rows) {
- var data = [],
- i;
- for (i = 1; i <= rows; ++i) {
- data.push({
- label: 'Item' + i,
- value: i
- });
- }
- store = new Ext.data.Store({
- model: Model,
- data: data
- });
- }
-
- beforeEach(function() {
- // Tons of warnings regarding Sencha download server in the console
- spyOn(Ext.log, 'warn');
- });
- afterEach(function() {
- store = chart = Ext.destroy(chart, store);
- // cleanup gradients
- Ext.draw.gradient.GradientDefinition.gradients = {};
- });
- it('is defined', function() {
- expect(Ext.chart.AbstractChart).toBeDefined();
- });
- describe("stores", function() {
- function makeChart(storeOnSeries, chartCfg, seriesCfg) {
- var cfg = Ext.apply({
- xtype: 'cartesian',
- axes: [{
- type: 'numeric',
- position: 'left'
- }, {
- type: 'category',
- position: 'bottom'
- }],
- animation: false,
- series: Ext.apply({
- type: 'bar',
- xField: 'label',
- yField: 'value'
- }, seriesCfg)
- }, chartCfg);
- if (storeOnSeries) {
- if (!cfg.series.store) {
- cfg.series.store = makeStore(3);
- }
- } else {
- if (!cfg.store) {
- cfg.store = makeStore(3);
- }
- }
- chart = new Ext.chart.CartesianChart(cfg);
- }
- function extractHasListeners(o) {
- var ret = {},
- key;
- for (key in o) {
- ret[key] = o[key];
- }
- delete ret._decr_;
- delete ret._incr_;
- return ret;
- }
- describe("store on the chart", function() {
- function makeStoreChart(chartCfg, seriesCfg) {
- makeChart(false, chartCfg, seriesCfg);
- }
- describe("configuration", function() {
- it("should accept a store id", function() {
- store = new Ext.data.Store({
- model: Model,
- storeId: 'foo'
- });
- makeStoreChart({
- store: 'foo'
- });
- expect(chart.getStore()).toBe(store);
- });
- it("should accept a store config", function() {
- makeStoreChart({
- store: {
- model: Model,
- data: [{}]
- }
- });
- expect(chart.getStore().getCount()).toBe(1);
- expect(chart.getStore().getModel()).toBe(Model);
- });
- it("should accept a store instance", function() {
- makeStore(10);
- makeStoreChart({
- store: store
- });
- expect(chart.getStore()).toBe(store);
- });
- });
- describe("destruction", function() {
- it("should remove all listeners", function() {
- makeStore(3);
- var listeners = extractHasListeners(store.hasListeners);
- makeStoreChart({
- store: store
- });
- chart.destroy();
- expect(extractHasListeners(store.hasListeners)).toEqual(listeners);
- });
- it("should not destroy the store by default", function() {
- makeStore(3);
- makeStoreChart({
- store: store
- });
- chart.destroy();
- expect(store.destroyed).toBe(false);
- });
- it("should destroy the store when the store has autoDestroy: true", function() {
- makeStore(3);
- store.setAutoDestroy(true);
- makeStoreChart({
- store: store
- });
- chart.destroy();
- expect(store.destroyed).toBe(true);
- });
- });
- describe("change", function () {
- it("should fire 'storechange' event", function () {
- var isFired = false,
- store1 = new Ext.data.Store({
- model: Model
- }),
- store2 = new Ext.data.Store({
- model: Model
- }),
- param1, param2, param3;
- makeStoreChart({
- store: store1
- });
- chart.on('storechange', function (chart, newStore, oldStore) {
- isFired = true;
- param1 = chart;
- param2 = newStore;
- param3 = oldStore;
- });
- chart.setStore(store2);
- expect(isFired).toEqual(true);
- expect(param1).toEqual(chart);
- expect(param2).toEqual(store2);
- expect(param3).toEqual(store1);
- });
- });
- });
- describe("store on the series", function() {
- function makeSeriesChart(chartCfg, seriesCfg) {
- makeChart(true, chartCfg, seriesCfg);
- }
- describe("configuration", function() {
- it("should accept a store id", function() {
- store = new Ext.data.Store({
- model: Model,
- storeId: 'foo'
- });
- makeSeriesChart(null, {
- store: 'foo'
- });
- expect(chart.getStore().isEmptyStore).toBe(true);
- expect(chart.getSeries()[0].getStore()).toBe(store);
- });
- it("should accept a store config", function() {
- makeSeriesChart(null, {
- store: {
- model: Model,
- data: [{}]
- }
- });
- expect(chart.getStore().isEmptyStore).toBe(true);
- expect(chart.getSeries()[0].getStore().getCount()).toBe(1);
- expect(chart.getSeries()[0].getStore().getModel()).toBe(Model);
- });
- it("should accept a store instance", function() {
- makeStore(10);
- makeSeriesChart(null, {
- store: store
- });
- expect(chart.getStore().isEmptyStore).toBe(true);
- expect(chart.getSeries()[0].getStore()).toBe(store);
- });
- });
- describe("destruction", function() {
- it("should remove all listeners", function() {
- makeStore(3);
- var listeners = extractHasListeners(store.hasListeners);
- makeSeriesChart(null, {
- store: store
- });
- chart.destroy();
- expect(extractHasListeners(store.hasListeners)).toEqual(listeners);
- });
- it("should not destroy the store by default", function() {
- makeStore(3);
- makeSeriesChart(null, {
- store: store
- });
- chart.destroy();
- expect(store.destroyed).toBe(false);
- });
- it("should destroy the store when the store has autoDestroy: true", function() {
- makeStore(3);
- store.setAutoDestroy(true);
- makeSeriesChart(null, {
- store: store
- });
- chart.destroy();
- expect(store.destroyed).toBe(true);
- });
- it("should not destroy the store when destroying the series by default", function() {
- makeStore(3);
- makeSeriesChart(null, {
- store: store
- });
- chart.setSeries([{
- type: 'bar',
- xField: 'label',
- yField: 'value'
- }]);
- expect(store.destroyed).toBe(false);
- });
- it("should destroy the store when destroying the series when the store has autoDestroy: true", function() {
- makeStore(3);
- store.setAutoDestroy(true);
- makeSeriesChart(null, {
- store: store
- });
- chart.setSeries([{
- type: 'bar',
- xField: 'label',
- yField: 'value'
- }]);
- expect(store.destroyed).toBe(true);
- });
- });
- describe("change", function () {
- it("should fire 'storechange' event", function () {
- var isFired = false,
- store1 = new Ext.data.Store({
- model: Model
- }),
- store2 = new Ext.data.Store({
- model: Model
- }),
- series, param1, param2, param3;
- makeSeriesChart(null, {
- store: store1
- });
- series = chart.getSeries()[0];
- series.on('storechange', function (series, newStore, oldStore) {
- isFired = true;
- param1 = series;
- param2 = newStore;
- param3 = oldStore;
- });
- series.setStore(store2);
- expect(isFired).toEqual(true);
- expect(param1).toEqual(series);
- expect(param2).toEqual(store2);
- expect(param3).toEqual(store1);
- });
- });
- });
- });
- describe('adding and removing series', function() {
- var layoutDone;
- beforeEach(function() {
- store = new Ext.data.Store({
- fields: ['x', 'y', 'z'],
- data: [
- {x: 0, y: 0, z: 0},
- {x: 1, y: 1, z: 1}
- ]
- });
- chart = new Ext.chart.CartesianChart({
- renderTo: Ext.getBody(),
- width: 400,
- height: 400,
- store: store,
- axes: [{
- position: 'left',
- type: 'numeric'
- }, {
- position: 'bottom',
- type: 'numeric'
- }],
- listeners: {
- layout: function () {
- layoutDone = true;
- }
- }
- });
- });
- afterEach(function () {
- layoutDone = false;
- });
- it('should start with no series', function() {
- expect(chart.getSeries().length).toBe(0);
- });
- it('should add and remove series using setSeries', function() {
- var series;
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- chart.setSeries([{
- type: 'line',
- xField: 'x',
- yField: 'y',
- id: 'xySeries'
- }]);
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(1);
- expect(series[0].getId()).toBe('xySeries');
- chart.setSeries([{
- type: 'line',
- xField: 'x',
- yField: 'z',
- id: 'xzSeries'
- }]);
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(1);
- expect(series[0].getId()).toBe('xzSeries');
- });
- });
- it('should add series using addSeries', function() {
- var series;
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- chart.addSeries([{
- type: 'line',
- xField: 'x',
- yField: 'y',
- id: 'xySeries'
- }]);
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(1);
- expect(series[0].getId()).toBe('xySeries');
- chart.addSeries({
- type: 'line',
- xField: 'x',
- yField: 'z',
- id: 'xzSeries'
- });
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(2);
- expect(series[0].getId()).toBe('xySeries');
- expect(series[1].getId()).toBe('xzSeries');
- });
- });
- it('should remove series using removeSeries', function() {
- var series;
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- chart.addSeries([{
- type: 'line',
- xField: 'x',
- yField: 'y',
- id: 'xySeries'
- }, {
- type: 'line',
- xField: 'x',
- yField: 'z',
- id: 'xzSeries'
- }]);
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(2);
- expect(series[0].getId()).toBe('xySeries');
- expect(series[1].getId()).toBe('xzSeries');
- // Remove Series id "xySeries", should leave only "xzSeries"
- chart.removeSeries('xySeries');
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- series = chart.getSeries();
- expect(series.length).toBe(1);
- expect(series[0].getId()).toBe('xzSeries');
- // Remove a Series by specifying the instance should leav no Series
- chart.removeSeries(series[0]);
- });
- waitsFor(function () {
- return layoutDone;
- });
- runs(function () {
- layoutDone = false;
- expect(chart.getSeries().length).toBe(0);
- });
- });
- });
- describe('getInteraction', function () {
- it("should return a correct interaction based on its type", function () {
- makeStore(3);
- chart = new Ext.chart.CartesianChart({
- store: store,
- interactions: [
- {
- type: 'itemhighlight'
- },
- {
- type: 'itemedit'
- },
- {
- type: 'crosszoom'
- }
- ],
- axes: [{
- type: 'numeric',
- position: 'left'
- }, {
- type: 'category',
- position: 'bottom'
- }],
- series: {
- type: 'bar',
- xField: 'label',
- yField: 'value'
- }
- });
- var itemhighlight = chart.getInteraction('itemhighlight'),
- crosszoom = chart.getInteraction('crosszoom'),
- itemedit = chart.getInteraction('itemedit');
- expect(itemhighlight.isItemHighlight).toBe(true);
- expect(crosszoom.isCrossZoom).toBe(true);
- expect(itemedit.isItemEdit).toBe(true);
- });
- });
- describe('processData', function () {
- it('should refresh legend store', function () {
- var layoutEnd, processDataSpy;
- runs(function () {
- chart = new Ext.chart.PolarChart({
- animation: false,
- renderTo: document.body,
- width: 400,
- height: 400,
- legend: {
- docked: 'right'
- },
- store: {
- data: [{
- "name": "A",
- "data1": 1
- }, {
- "name": "B",
- "data1": 2
- }]
- },
- series: {
- type: 'pie3d',
- angleField: 'data1',
- label: {
- field: 'name'
- }
- },
- listeners: {
- layout: function () {
- layoutEnd = true;
- }
- }
- });
- });
- waitsFor(function () {
- return layoutEnd;
- });
- runs(function () {
- layoutEnd = false;
- expect(chart.getLegend().getStore().getAt(0).get('name')).toBe('A');
- processDataSpy = spyOn(chart, 'processData').andCallThrough();
- chart.getStore().loadData([{
- name: 'X',
- data1: 24
- }, {
- name: 'Y',
- data1: 25
- }]);
- expect(processDataSpy).toHaveBeenCalled();
- expect(chart.getLegend().getStore().getAt(0).get('name')).toBe('X');
- });
- });
- });
- describe("update gradients", function () {
- beforeEach(function () {
- makeStore(3);
- chart = new Ext.chart.CartesianChart({
- store: store,
- axes: [{
- type: 'numeric',
- position: 'left'
- }, {
- type: 'category',
- position: 'bottom'
- }],
- series: {
- type: 'bar',
- xField: 'label',
- yField: 'value',
- style: {
- fillStyle: 'url(#foo)'
- }
- },
- gradients: [{
- id: 'foo',
- type: 'linear',
- degrees: 270,
- stops: [{
- offset: 0,
- color: '#78C5D6'
- }, {
- offset: 0.56,
- color: '#F5D63D'
- }, {
- offset: 1,
- color: '#BF62A6'
- }]
- }]
- });
- });
- it("should create sprites with the correct gradient applied", function () {
- var seriesSprite = chart.getSeries()[0].sprites[0],
- fillStyle = seriesSprite.attr.fillStyle;
- expect(typeof fillStyle).toBe('object');
- expect(fillStyle.isGradient).toBe(true);
- });
- it("should update sprites when gradient has been updated", function () {
- var seriesSprite = chart.getSeries()[0].sprites[0],
- fillStyle = seriesSprite.attr.fillStyle,
- newGradient = [{
- id: 'foo',
- type: 'linear',
- degrees: 270,
- stops: [{
- offset: 0,
- color: '#000000'
- }, {
- offset: 0.56,
- color: '#F5D63D'
- }, {
- offset: 1,
- color: '#000000'
- }]
- }];
- // first, make sure correct color is applied on initial construction
- expect(fillStyle.getStops()[0].color).toBe('#78c5d6');
- // now let's update the draw container with a slightly different gradient with the same id
- chart.setGradients(newGradient);
- // theme should get updated; check sprite again
- seriesSprite = chart.getSeries()[0].sprites[0];
- fillStyle = seriesSprite.attr.fillStyle;
- // should have different colors for updated gradient
- expect(fillStyle.getStops()[0].color).toBe('#000000');
- });
- });
- describe('legend render checking if legend is getting rendered along with Chart', function () {
- afterEach(function() {
- chart = Ext.destroy(chart);
- });
- it("legend should be rendered", function () {
- runs(function () {
- chart = Ext.create({
- xtype: 'cartesian',
- renderTo: Ext.getBody(),
- width: 400,
- height: 400,
- innerPadding: 10,
- store: {
- fields: ['name', 'data1'],
- data: [[{
- age: '0',
- value: 400
- }, {
- age: '2',
- value: 150
- }, {
- age: '4',
- value: 120
- }, {
- age: '6',
- value: 100
- }, {
- age: '8',
- value: 1500
- }]]
- },
- legend: {
- type: 'dom',
- docked: 'right'
- },
- series: {
- type: 'pie',
- highlight: true,
- angleField: 'data1',
- donut: 30
- }
- });
- });
- runs(function () {
- var legend = chart.getLegend();
- expect(legend.rendered).toBe(true);
- });
- });
- });
- });
|