@bizcharts/g2-brush-highlight
Version:
Bizhcarts basic charts
270 lines (231 loc) • 9.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _bizcharts = require("bizcharts");
var _g2Brush = require("@antv/g2-brush");
var _g2Brush2 = _interopRequireDefault(_g2Brush);
var _jquery = require("jquery");
var _jquery2 = _interopRequireDefault(_jquery);
var _mock = require("./mock.json");
var _mock2 = _interopRequireDefault(_mock);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function getComponent(data) {
var Util = _bizcharts.G2.Util;
var scale = {
Species: {
sync: true
}
};
var chart = void 0;
var brush = void 0;
function setBrushType(type) {
if (!brush) {
brush = new _g2Brush2.default({
canvas: chart.get("canvas"),
dragable: true,
type: type,
onBrushstart: function onBrushstart(ev) {
chart.hideTooltip();
var x = ev.x,
y = ev.y;
var views = chart.getViewsByPoint({
x: x,
y: y
});
if (views.length > 1) {
this.chart = views[1];
var coord = views[1].get("coord");
this.plot = {
start: coord.start,
end: coord.end
};
this.xScale = views[1].getXScale();
this.yScale = views[1].getYScales()[0];
}
},
onBrushmove: function onBrushmove(ev) {
chart.hideTooltip();
var data = ev.data;
chart.eachShape(function (record, shape) {
if (!shape.get("_originAttrs")) {
shape.set("_originAttrs", Util.cloneDeep(shape.__attrs)); // 缓存原来的属性
}
if (data.indexOf(record) === -1) {
shape.attr("fill", "#ccc");
} else {
var originAttrs = shape.get("_originAttrs");
shape.__attrs = Util.cloneDeep(originAttrs);
}
});
},
onDragmove: function onDragmove(ev) {
chart.hideTooltip();
var data = ev.data;
chart.eachShape(function (record, shape) {
if (!shape.get("_originAttrs")) {
shape.set("_originAttrs", Util.cloneDeep(shape.__attrs)); // 缓存原来的属性
}
if (data.indexOf(record) === -1) {
shape.attr("fill", "#ccc");
} else {
var originAttrs = shape.get("_originAttrs");
shape.__attrs = Util.cloneDeep(originAttrs);
}
});
}
});
} else {
if (type === "clear") {
brush.container.clear(); // brush.canvas.draw();
} else {
brush.setType(type);
}
}
}
var RenderChart = function (_React$Component) {
_inherits(RenderChart, _React$Component);
function RenderChart() {
_classCallCheck(this, RenderChart);
return _possibleConstructorReturn(this, (RenderChart.__proto__ || Object.getPrototypeOf(RenderChart)).apply(this, arguments));
}
_createClass(RenderChart, [{
key: "componentDidMount",
value: function componentDidMount() {
(0, _jquery2.default)("#XY").click(function () {
setBrushType("XY");
});
(0, _jquery2.default)("#Y").click(function () {
setBrushType("Y");
});
(0, _jquery2.default)("#X").click(function () {
setBrushType("X");
});
(0, _jquery2.default)("#POLYGON").click(function () {
setBrushType("POLYGON");
});
(0, _jquery2.default)("#clear").click(function () {
setBrushType("clear");
chart.eachShape(function (record, shape) {
if (shape.get("_originAttrs")) {
var originAttrs = shape.get("_originAttrs");
shape.__attrs = Util.cloneDeep(originAttrs);
}
});
});
}
}, {
key: "render",
value: function render() {
return _react2.default.createElement(
"div",
null,
_react2.default.createElement(
_bizcharts.Chart,
{
height: window.innerHeight,
data: data,
scale: scale,
onGetG2Instance: function onGetG2Instance(g2Chart) {
g2Chart.animate(false);
chart = g2Chart;
},
forceFit: true
},
_react2.default.createElement(_bizcharts.Axis, { name: "rain", grid: null }),
_react2.default.createElement(_bizcharts.Axis, { name: "flow", title: true }),
_react2.default.createElement(_bizcharts.Tooltip, null),
_react2.default.createElement(_bizcharts.Legend, { hoverable: false }),
_react2.default.createElement(_bizcharts.Facet, {
type: "matrix",
fields: ["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"],
eachView: function eachView(view, facet) {
view.axis(facet.colField, {
label: null,
line: {
lineWidth: 1,
stroke: "#000"
},
tickLine: {
lineWidth: 1,
stroke: "#000",
length: 4
}
});
view.axis(facet.rowField, {
label: null,
line: {
lineWidth: 1,
stroke: "#000"
},
tickLine: {
lineWidth: 1,
stroke: "#000",
length: 4
}
});
if (facet.rowIndex === facet.colIndex) {
view.point().position(facet.colField + "*" + facet.colField).color("Species", ["#880000", "#008800", "#000088"]).opacity(0.5).shape("circle").size(3).active(false);
} else {
view.point().position([facet.colField, facet.rowField]).color("Species", ["#880000", "#008800", "#000088"]).opacity(0.5).shape("circle").size(3).active(false);
}
if ([0, 1, 2, 3].indexOf(facet.rowIndex) > -1 && facet.colIndex === 0) {
view.guide().text({
position: [3.7, "median"],
content: facet.rowValue,
style: {
rotate: -90,
fontSize: 12,
fill: "#999",
textAlign: "center"
}
});
}
if ([0, 1, 2, 3].indexOf(facet.colIndex) > -1 && facet.rowIndex === 3) {
view.guide().text({
position: ["median", "min"],
content: facet.colValue,
style: {
fontSize: 12,
fill: "#999",
textAlign: "center"
},
offsetY: 20
});
}
}
})
)
);
}
}]);
return RenderChart;
}(_react2.default.Component);
return RenderChart;
}
var Brushhighlight = function (_React$Component2) {
_inherits(Brushhighlight, _React$Component2);
function Brushhighlight() {
_classCallCheck(this, Brushhighlight);
return _possibleConstructorReturn(this, (Brushhighlight.__proto__ || Object.getPrototypeOf(Brushhighlight)).apply(this, arguments));
}
_createClass(Brushhighlight, [{
key: "render",
value: function render() {
var RenderChart = getComponent(_mock2.default);
return _react2.default.createElement(
"div",
null,
_react2.default.createElement(RenderChart, null)
);
}
}]);
return Brushhighlight;
}(_react2.default.Component);
exports.default = Brushhighlight;