Browse Source

Fix data label

pull/2246/head
Masayuki Tanaka 7 years ago
parent
commit
8de2b509f5
  1. 2
      htdocs/samples/data_label.html
  2. 19
      src/text.js

2
htdocs/samples/data_label.html

@ -15,7 +15,7 @@
<div id="chart10"></div> <div id="chart10"></div>
<div id="chart11"></div> <div id="chart11"></div>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
<script src="/js/c3.js"></script> <script src="/js/c3.js"></script>
<script> <script>

19
src/text.js

@ -8,35 +8,36 @@ c3_chart_internal_fn.initText = function () {
$$.mainText = $$.d3.selectAll([]); $$.mainText = $$.d3.selectAll([]);
}; };
c3_chart_internal_fn.updateTargetsForText = function (targets) { c3_chart_internal_fn.updateTargetsForText = function (targets) {
var $$ = this, mainTextUpdate, mainTextEnter, var $$ = this,
classChartText = $$.classChartText.bind($$), classChartText = $$.classChartText.bind($$),
classTexts = $$.classTexts.bind($$), classTexts = $$.classTexts.bind($$),
classFocus = $$.classFocus.bind($$); classFocus = $$.classFocus.bind($$);
mainTextUpdate = $$.main.select('.' + CLASS.chartTexts).selectAll('.' + CLASS.chartText) var mainText = $$.main.select('.' + CLASS.chartTexts).selectAll('.' + CLASS.chartText)
.data(targets) .data(targets);
.attr('class', function (d) { return classChartText(d) + classFocus(d); }); var mainTextEnter = mainText.enter().append('g')
mainTextEnter = mainTextUpdate.enter().append('g')
.attr('class', classChartText) .attr('class', classChartText)
.style('opacity', 0) .style('opacity', 0)
.style("pointer-events", "none"); .style("pointer-events", "none");
mainTextEnter.append('g') mainTextEnter.append('g')
.attr('class', classTexts); .attr('class', classTexts);
var mainTextUpdate = mainTextEnter.merge(mainText)
.attr('class', function (d) { return classChartText(d) + classFocus(d); });
}; };
c3_chart_internal_fn.updateText = function (durationForExit) { c3_chart_internal_fn.updateText = function (durationForExit) {
var $$ = this, config = $$.config, var $$ = this, config = $$.config,
barOrLineData = $$.barOrLineData.bind($$), barOrLineData = $$.barOrLineData.bind($$),
classText = $$.classText.bind($$); classText = $$.classText.bind($$);
$$.mainText = $$.main.selectAll('.' + CLASS.texts).selectAll('.' + CLASS.text) var mainText = $$.main.selectAll('.' + CLASS.texts).selectAll('.' + CLASS.text)
.data(barOrLineData); .data(barOrLineData);
$$.mainText.enter().append('text') var mainTextEnter = mainText.enter().append('text')
.attr("class", classText) .attr("class", classText)
.attr('text-anchor', function (d) { return config.axis_rotated ? (d.value < 0 ? 'end' : 'start') : 'middle'; }) .attr('text-anchor', function (d) { return config.axis_rotated ? (d.value < 0 ? 'end' : 'start') : 'middle'; })
.style("stroke", 'none') .style("stroke", 'none')
.style("fill", function (d) { return $$.color(d); }) .style("fill", function (d) { return $$.color(d); })
.style("fill-opacity", 0); .style("fill-opacity", 0);
$$.mainText $$.mainText = mainTextEnter.merge(mainText)
.text(function (d, i, j) { return $$.dataLabelFormat(d.id)(d.value, d.id, i, j); }); .text(function (d, i, j) { return $$.dataLabelFormat(d.id)(d.value, d.id, i, j); });
$$.mainText.exit() mainText.exit()
.transition().duration(durationForExit) .transition().duration(durationForExit)
.style('fill-opacity', 0) .style('fill-opacity', 0)
.remove(); .remove();

Loading…
Cancel
Save