如何使饼图和条形图未选中的部分变灰?

我正在尝试通过以下示例进行练习: 在此处输入链接描述

上面的链接显示当我在饼图中选择2011年时,其他都是灰色的。但是我的代码没有成功。

该示例完美运行,如下所示:

在此处输入图像描述

我下面的代码无法像示例所示那样使饼图和条形图的未选择部分变灰:

<!DOCTYPE html>

<html>

<head>

    <title>dc.js - Removing Empty Bars</title>

    <meta charset="UTF-8">

    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="https://gist.githubusercontent.com/kevinkraus92/cc5ac08b30c244e54e2c96bbe5fea110/raw/608ea6bc5564c9705a6b3c41281b5dddc84b8879/dc.css"/>

</head>

<body>


<div class="container">

<script type="text/javascript" src="https://raw.githubusercontent.com/dc-js/dc.js/develop/web-src/examples/header.js"></script>

  <p>Example demonstrating using a "<a href="https://github.com/dc-js/dc.js/wiki/FAQ#fake-groups">Fake Group</a>" to remove

    the empty bars of an ordinal bar chart when their values drop to zero.</p>


  <p>(Note the use of <code><a href="https://dc-js.github.io/dc.js/docs/html/CoordinateGridMixin.html#elasticX">.elasticX(true)</a></code>

    to force calculation of the X domain each round.)</p>


<div id="chart-ring-year"></div>

<div id="chart-hist-spend"></div>

<div id="chart-row-spenders"></div>


<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.1.1/d3.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/4.1.1/dc.js"></script>

<script type="text/javascript">


var yearRingChart   = new dc.PieChart("#chart-ring-year"),

    spendHistChart  = new dc.BarChart("#chart-hist-spend"),

    spenderRowChart = new dc.RowChart("#chart-row-spenders");



];


// normalize/parse data

spendData.forEach(function(d) {

    d.Spent = d.Spent.match(/\d+/);

});

有什么想法吗?谢谢。




潇湘沐
浏览 67回答 1
1回答

波斯汪

使用 dc.js 的 CSS 可以获得相同的效果。如果您想为其设置样式,请使用该 CSS 作为起点。<link rel="stylesheet" type="text/css" href="https://dc-js.github.io/dc.js/css/dc.css"/><!DOCTYPE html><html><head>&nbsp; &nbsp; <title>dc.js - Removing Empty Bars</title>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="https://dc-js.github.io/dc.js/css/dc.css"/></head><body><div class="container"><script type="text/javascript" src="https://raw.githubusercontent.com/dc-js/dc.js/develop/web-src/examples/header.js"></script>&nbsp; <p>Example demonstrating using a "<a href="https://github.com/dc-js/dc.js/wiki/FAQ#fake-groups">Fake Group</a>" to remove&nbsp; &nbsp; the empty bars of an ordinal bar chart when their values drop to zero.</p>&nbsp; <p>(Note the use of <code><a href="https://dc-js.github.io/dc.js/docs/html/CoordinateGridMixin.html#elasticX">.elasticX(true)</a></code>&nbsp; &nbsp; to force calculation of the X domain each round.)</p><div id="chart-ring-year"></div><div id="chart-hist-spend"></div><div id="chart-row-spenders"></div><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.1.1/d3.min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/4.1.1/dc.js"></script><script type="text/javascript">var yearRingChart&nbsp; &nbsp;= new dc.PieChart("#chart-ring-year"),&nbsp; &nbsp; spendHistChart&nbsp; = new dc.BarChart("#chart-hist-spend"),&nbsp; &nbsp; spenderRowChart = new dc.RowChart("#chart-row-spenders");// use static or load via d3.csv("spendData.csv").then(function(spendData) {/* do stuff */});var spendData = [&nbsp; &nbsp; {Name: 'Mr A', Spent: '$40', Year: 2011},&nbsp; &nbsp; {Name: 'Mr B', Spent: '$10', Year: 2011},&nbsp; &nbsp; {Name: 'Mr C', Spent: '$40', Year: 2011},&nbsp; &nbsp; {Name: 'Mr A', Spent: '$70', Year: 2012},&nbsp; &nbsp; {Name: 'Mr B', Spent: '$20', Year: 2012},&nbsp; &nbsp; {Name: 'Mr B', Spent: '$50', Year: 2013},&nbsp; &nbsp; {Name: 'Mr C', Spent: '$30', Year: 2013}];// normalize/parse dataspendData.forEach(function(d) {&nbsp; &nbsp; d.Spent = d.Spent.match(/\d+/);});function remove_empty_bins(source_group) {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; all:function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return source_group.all().filter(function(d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d.value != 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}// set crossfiltervar ndx = crossfilter(spendData),&nbsp; &nbsp; yearDim&nbsp; = ndx.dimension(function(d) {return +d.Year;}),&nbsp; &nbsp; spendDim = ndx.dimension(function(d) {return Math.floor(d.Spent/10);}),&nbsp; &nbsp; nameDim&nbsp; = ndx.dimension(function(d) {return d.Name;}),&nbsp; &nbsp; spendPerYear = yearDim.group().reduceSum(function(d) {return +d.Spent;}),&nbsp; &nbsp; spendPerName = nameDim.group().reduceSum(function(d) {return +d.Spent;}),&nbsp; &nbsp; spendHist&nbsp; &nbsp; = spendDim.group().reduceCount(),&nbsp; &nbsp; nonEmptyHist = remove_empty_bins(spendHist)yearRingChart&nbsp; &nbsp; .width(200).height(200)&nbsp; &nbsp; .dimension(yearDim)&nbsp; &nbsp; .group(spendPerYear)&nbsp; &nbsp; .innerRadius(50);spendHistChart&nbsp; &nbsp; .width(300).height(200)&nbsp; &nbsp; .dimension(spendDim)&nbsp; &nbsp; .group(nonEmptyHist)&nbsp; &nbsp; .x(d3.scaleBand())&nbsp; &nbsp; .xUnits(dc.units.ordinal)&nbsp; &nbsp; .elasticX(true)&nbsp; &nbsp; .elasticY(true);spendHistChart.xAxis().tickFormat(function(d) {return d*10}); // convert back to base unitspendHistChart.yAxis().ticks(2);spenderRowChart&nbsp; &nbsp; .width(350).height(200)&nbsp; &nbsp; .dimension(nameDim)&nbsp; &nbsp; .group(spendPerName)&nbsp; &nbsp; .elasticX(true);dc.renderAll();</script></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript