花点时间检查下面的气泡图。这是我在Illustrator中创建的图像。我现在正尝试使用dc.js复制气泡图。
通过剖析this page上给出的示例图的代码,我试图实现上述目标。这是我的代码:
'use strict'
// define bubble chart and get a handle
var referralsChart = dc.bubbleChart('#referrals-bubble-chart');
// load the csv data
d3.csv("data/data.csv", function (data) {
/* since its a csv file we need to format the data a bit */
var dateFormat = d3.time.format("%d/%m/%Y");
var numberFormat = d3.format(".2f");
data.forEach(function (d) {
d.dd = dateFormat.parse(d.date);
});
// create cross-filter dimensions and groups
// see the crossfilter api at (https://github.com/square/crossfilter/wiki/API-Reference) for reference.
var referralsChartXFilter = crossfilter(data);
var referralsChartXGroups = referralsChartXFilter.groupAll();
// dimension by full date
var dateDimension = referralsChartXFilter.dimension(function (d) {
return d.dd.getMonth();
});
var diagnosisDimension = referralsChartXFilter.dimension(function (d) {
return d.referredfor;
});
// dimension group
var diagnosisDimensionGroup = diagnosisDimension.group();
// define the referrals bubble chart attributes
referralsChart
.width(700)
.height(400)
.transitionDuration(1500) // (optional) define chart transition duration, :default = 750
.margins({top: 10, right: 50, bottom: 30, left: 40})
.dimension(dateDimension)
//Bubble chart expect the groups are reduced to multiple values which would then be used
//to generate x, y, and radius for each key (bubble) in the group
.group(diagnosisDimensionGroup)
.colors(colorbrewer.RdYlGn[9]) // (optional) define color function or array for bubbles
.colorDomain([0, 100]) //(optional) define color domain to match your data domain if you want to bind data or color
//##### Accessors
//Accessor functions are applied to each value returned by the grouping
//
//* `.colorAccessor` The returned value will be mapped to an internal scale to determine a fill color
//* `.keyAccessor` Identifies the `X` value that will be applied against the `.x()` to identify pixel location
//* `.valueAccessor` Identifies the `Y` value that will be applied agains the `.y()` to identify pixel location
//* `.radiusValueAccessor` Identifies the value that will be applied agains the `.r()` determine radius size, by default this maps linearly to [0,100]
.colorAccessor(function (d) {
return d.value / 100
})
.keyAccessor(function (p) {
return p.value
})
.valueAccessor(function (p) {
return p.value
})
.radiusValueAccessor(function (p) {
return p.value
})
.maxBubbleRelativeSize(0.1)
.x(d3.scale.linear().domain([0, 5000]))
.y(d3.scale.linear().domain([0, 5000]))
.r(d3.scale.linear().domain([0, 4000]))
//##### Elastic Scaling
//`.elasticX` and `.elasticX` determine whether the chart should rescale each axis to fit data.
//The `.yAxisPadding` and `.xAxisPadding` add padding to data above and below their max values in the same unit domains as the Accessors.
.elasticY(true)
.elasticX(true)
.yAxisPadding(100)
.xAxisPadding(500)
.renderHorizontalGridLines(true) // (optional) render horizontal grid lines, :default=false
.renderVerticalGridLines(true) // (optional) render vertical grid lines, :default=false
.xAxisLabel('Date') // (optional) render an axis label below the x axis
.yAxisLabel('Referrals') // (optional) render a vertical axis lable left of the y axis
//#### Labels and Titles
//Labels are displaed on the chart for each bubble. Titles displayed on mouseover.
.renderLabel(true) // (optional) whether chart should render labels, :default = true
.label(function (p) {
return p.key;
})
.renderTitle(true) // (optional) whether chart should render titles, :default = false
.title(function (p) {
return [p.key,
"Referrals: " + numberFormat(p.value.absGain),
"Index Gain in Percentage: " + numberFormat(p.value.percentageGain) + "%",
"Fluctuation / Index Ratio: " + numberFormat(p.value.fluctuationPercentage) + "%"]
.join("\n");
})
//#### Customize Axis
//Set a custom tick format. Note `.yAxis()` returns an axis object, so any additional method chaining applies to the axis, not the chart.
.yAxis().tickFormat(function (v) {
return v + "%";
});
dc.renderAll();
});
以上代码的工作原理是我绘制了气泡,但x轴和y轴显示错误的刻度。以下是我使用上述代码获得的屏幕截图:
我想要的是按月绘制的x轴(1月,2月,3月等)和 y轴的数据范围介
传递的csv数据可以是found here。
我知道我需要编辑下面的两行,但我不确定如何将数据传递给他们。
.x(d3.scale.linear().domain([0, 5000]))
.y(d3.scale.linear().domain([0, 5000]))
.r(d3.scale.linear().domain([0, 4000]))
答案 0 :(得分:3)
您需要将.x
设置为d3.time.scale
https://github.com/dc-js/dc.js/blob/master/web/docs/api-latest.md#xxscale---mandatory https://github.com/mbostock/d3/wiki/Time-Scales
您似乎还想设置.r
以反映您的数据。
另外,您可能不需要设置缩放对象的.domain
,因为您使用的是.elasticX
和.elasticY
,它们会自动设置这些对象。
您可能还希望类似的.elasticRadius
根据当前值调整气泡的大小。
https://github.com/dc-js/dc.js/blob/master/web/docs/api-latest.md#elasticradiusboolean