Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/vislib click error #31896

Merged
merged 2 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/legacy/ui/public/vislib/__tests__/lib/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@ describe('Vislib Dispatch Class Test Suite', function () {
});
});
});

describe('clickEvent handler', () => {
describe('for pie chart', () => {
it ('prepares data points', () => {
const expectedResponse = [ { column: 0, row: 0, table: {}, value: 0 } ];
const d = { rawData: { column: 0, row: 0, table: {}, value: 0 } };
const chart = _.first(vis.handler.charts);
const response = chart.events.clickEventResponse(d, { isSlices: true });
expect(response.data).to.eql(expectedResponse);
});

it ('remove invalid points', () => {
const expectedResponse = [ { column: 0, row: 0, table: {}, value: 0 } ];
const d = { rawData: { column: 0, row: 0, table: {}, value: 0 }, yRaw: { table: {}, value: 0 } };
const chart = _.first(vis.handler.charts);
const response = chart.events.clickEventResponse(d, { isSlices: true });
expect(response.data).to.eql(expectedResponse);
});
});

describe('for xy charts', () => {
it ('prepares data points', () => {
const expectedResponse = [ { column: 0, row: 0, table: {}, value: 0 } ];
const d = { xRaw: { column: 0, row: 0, table: {}, value: 0 } };
const chart = _.first(vis.handler.charts);
const response = chart.events.clickEventResponse(d, { isSlices: false });
expect(response.data).to.eql(expectedResponse);
});

it ('remove invalid points', () => {
const expectedResponse = [ { column: 0, row: 0, table: {}, value: 0 } ];
const d = { xRaw: { column: 0, row: 0, table: {}, value: 0 }, yRaw: { table: {}, value: 0 } };
const chart = _.first(vis.handler.charts);
const response = chart.events.clickEventResponse(d, { isSlices: false });
expect(response.data).to.eql(expectedResponse);
});
});
});
});

describe('Custom event handlers', function () {
Expand Down
21 changes: 12 additions & 9 deletions src/legacy/ui/public/vislib/lib/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import d3 from 'd3';
import _ from 'lodash';
import { get } from 'lodash';
import $ from 'jquery';
import { SimpleEmitter } from '../../utils/simple_emitter';

Expand Down Expand Up @@ -49,8 +49,8 @@ export function VislibLibDispatchProvider(Private, config) {
dataPointer = dataPointer.parent;
}

if (data.rawData.table.$parent) {
const { table, column, row, key } = data.rawData.table.$parent;
if (get(data, 'rawData.table.$parent')) {
const { table, column, row, key } = get(data, 'rawData.table.$parent');
points.push({ table, column, row, value: key });
}

Expand All @@ -61,7 +61,7 @@ export function VislibLibDispatchProvider(Private, config) {
const points = [];

['xRaw', 'yRaw', 'zRaw', 'seriesRaw', 'rawData', 'tableRaw'].forEach(val => {
if (data[val]) {
if (data[val] && data[val].column !== undefined && data[val].row !== undefined) {
points.push(data[val]);
}
});
Expand All @@ -75,10 +75,13 @@ export function VislibLibDispatchProvider(Private, config) {
* @param d {Object} Data point
* @returns event with list of data points related to the click
*/
clickEventResponse(d) {
const _data = d3.event.target.nearestViewportElement ?
d3.event.target.nearestViewportElement.__data__ : d3.event.target.__data__;
const isSlices = !!(_data && _data.slices);
clickEventResponse(d, props = {}) {
let isSlices = props.isSlices;
Copy link
Contributor

@flash1293 flash1293 Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just for testing to avoid mocking d3 here? If yes, the JSdoc of the function could be updated to reflect this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ... well it would be nice if we wouldn't use that d3 magic at all, but removing it would take much more time i guess

if (isSlices === undefined) {
const _data = d3.event.target.nearestViewportElement ?
d3.event.target.nearestViewportElement.__data__ : d3.event.target.__data__;
isSlices = !!(_data && _data.slices);
}

const data = d.input || d;

Expand Down Expand Up @@ -107,7 +110,7 @@ export function VislibLibDispatchProvider(Private, config) {
const series = isSeries ? data.series : undefined;
const slices = isSlices ? data.slices : undefined;
const handler = this.handler;
const color = _.get(handler, 'data.color');
const color = get(handler, 'data.color');

const eventData = {
value: d.y,
Expand Down