D3中的轴和网格的颜色不同

时间:2015-09-01 09:10:45

标签: javascript css d3.js

我想要轴的不同颜色(x和y)和图形的网格。我试过改变CSS但是无法改变它。

enter image description here

我已附上图形代码和css。 css代码在

之下
.axis path,
.axis line {
    fill: none;
    /*stroke: #000;*/
    stroke: #B2B2B2;
    shape-rendering: crispEdges;
    opacity: 0.5;
}

.area_characteristics_temperature {
    fill: steelblue;
    opacity:0.5; 
    border: 0px;
    margin:0px;
    overflow: hidden;
} 
.area_characteristics_temperature_3yr {
    fill: #7da7ca;
    opacity:0.5; 
    border: 0px;
    margin:0px;
    overflow: hidden;
} 
.area_characteristics_temperature_10yr {
    fill: #676767;
    opacity:0.5; 
    border: 0px;
    margin:0px;
    overflow: hidden;
} 
.area_characteristics_temperature_prev {
    fill: #eec7ea;
    opacity:0.5; 
    border: 0px;
    margin:0px;
    overflow: hidden;
} 

.line {
    fill: transparent;
    stroke:red;
    stroke-width:3px;
}
.line1 {
    fill: transparent;
    stroke:green;
    stroke-width:3px;
}

.line_characteristics_current_temperature_max{
    fill: transparent;
    /*stroke:red;*/
    stroke:#CC3300;
    stroke-width:3px;
}

.line_characteristics_current_temperature_min{
    fill: transparent;
    /*stroke:yellow;*/
    stroke:#0099CC;
    stroke-width:3px;
}

.line_characteristics_forecast_temperature_max{
    fill: transparent;
    /*stroke:green;*/
    stroke:#CC3300;
    stroke-width:3px;
    stroke-height:3px;
    /*stroke-dasharray:2.5px,6px;*/
    stroke-dasharray:2.5px,5.5px,2.5px,5.5px;
    stroke-soft:5px;
    stroke-linecap:round;
    stroke-height:3px;
    stroke-width:3.4px;
}

.line_characteristics_forecast_temperature_min{
    fill: transparent;
    stroke:#0088C3;
    stroke-width:3px;
    stroke-height:3px;
    /*stroke-dasharray:3px,6px,3px,6px;*/
    stroke-dasharray:2.5px,5.5px,2.5px,5.5px;
    stroke-soft:5px;    
    stroke-linecap:round;
    stroke-height:3px;
    stroke-width:3.4px;
    strokeDashoffset:10px,5px;
}
#wgtmsr option{
    width:150px;   
}

/*<DEMO>*/
#nav {
    height:200px;
    width:100px;
    float:left;
    padding:5px;
    padding-bottom:50px; 
}
#section {
    width:350px;
    float:left;
    padding:10px;        
}

/* Main style*/
.widget_sidebar{
    min-width: 200px;
    max-width:25%;
    overflow:hidden;
    float:left
}
.widget_graphArea{
    min-width: 200px;
    max-width:75%;
    overflow:hidden;
}

图形代码:我使用D3进行图形编码

var self = this;
        this.divID = divID;
        this.forecastEnabled = true;
        this.currentEnabled = true;
        this.averagesEnabled = true;
        this.currentData = [];
        this.areaClass = "area_characteristics_temperature";

        this.averagesData = []


        this.forecastdata = new Array();
        this.merged_data = new Array();
        this.svg = null;

        this._currentDataTrail = {temperature_max: 0, temperature_min: 0, date: 0}
        this.parseDate = d3.time.format("%Y-%m-%d").parse;

        w = $("#" + this.divID).width();
        h = $("#" + this.divID).height();

        this.boxing = {top: 20, right: 30, bottom: 30, left: 50},
        this.width = w - this.boxing.left - this.boxing.right,
                this.height = h - this.boxing.top - this.boxing.bottom;

        this.x = d3.time.scale()
                .range([0, this.width]);

        this.y = d3.scale.linear()
                .range([this.height, 0]);

        this.xAxis = d3.svg.axis()
                .scale(this.x)
//                .ticks(d3.time.day, 1)
//                .tickFormat(d3.time.format('%b %d'))
                .orient("bottom");


        this.yAxis = d3.svg.axis()
                .scale(this.y)
                .orient("left")
                .ticks(5, 0)
                .tickPadding(10);

        this.svg = null;

        this.line_characteristics = d3.svg.line()
                .x(function (d) {
                    return self.x(d.date);
                })
                .y(function (d) {
                    return self.y(d.temperature);
                });

        this.line_characteristics_temperature_min = d3.svg.line()
                .x(function (d) {
                    return self.x(d.date);
                })
                .y(function (d) {
                    return self.y(d.temperature_min);
                });

        this.line_characteristics_temperature_max = d3.svg.line()
                .x(function (d) {
                    return self.x(d.date);
                })
                .y(function (d) {
                    return self.y(d.temperature_max);
                });

        this.area_characteristics_temperature = d3.svg.area()
                .x(function (d) {
                    return self.x(d.date);
                })
                .y0(function (d) {
                    return self.y(d.temperature_max);
                })
                .y1(function (d) {
                    return self.y(d.temperature_min);
                });


        self.updateCurrentData = function (newData, append)
        {
            var _currentMaxDate = new Date(0);
            newData.forEach(function (d) {
                d.date = self.parseDate(d.date);
                d.temperature_max = d.temperatures.max;
                d.temperature_min = d.temperatures.min;

                if (d.date > _currentMaxDate) {
                    _currentMaxDate = d.date;
                    self._currentDataTrail.temperature_max = d.temperature_max;
                    self._currentDataTrail.temperature_min = d.temperature_min;
                    self._currentDataTrail.date = d.date;
                }
            });
            if (append) {
                self.currentData = self.currentData.concat(newData);
            } else {
                self.currentData = newData;
            }
            self.forecastData = new Array();
            self.updateMerged();
        };

        self.setAreaClass = function (css_class) {
            self.areaClass = css_class;
        }

        self.updateForecastData = function (newData)
        {
            self.forecastData = new Array();
            newData.forEach(function (d) {
                d.date = self.parseDate(d.date);
                d.temperature_max = d.forecast[0].temperatures.max;
                d.temperature_min = d.forecast[0].temperatures.min;
            });

            if (self._currentDataTrail && self._currentDataTrail.date) {
                self.forecastData.push(self._currentDataTrail)
            }
            self.forecastData = self.forecastData.concat(newData)
            self.updateMerged();
        };

        self.updateAveragesData = function (newData, append)
        {

            var _temp = new Date();
            var current = new Date(_temp.getFullYear(), 0, 1);

            newData.forEach(function (d) {
                if (d.minTemp) {
                    current.setDate(current.getDate() + 1);
                    d.temperature_max = d.maxTemp.average.toFixed(4);
                    d.temperature_min = d.minTemp.average.toFixed(4);
                    var date = current.getFullYear() + "-" + d.day;
                    d.date = self.parseDate(date);
                } else {
                    d.date = self.parseDate(d.date);
                    d.date.setFullYear(d.date.getFullYear() + 1)
                    d.temperature_max = d.temperatures.max;
                    d.temperature_min = d.temperatures.min;
                }
            });
            if (append) {
                self.averagesData = self.averagesData.concat(newData);
            } else {
                self.averagesData = newData;
            }
            self.updateMerged();
        };




        self.updateMerged = function ()
        {
            self.merged_data = new Array();

            if (self.currentData != undefined && self.currentData instanceof Array && self.currentData.length > 0 && self.forecastData != undefined && self.forecastData instanceof Array && self.forecastData.length > 0) {
                self.merged_data = self.merged_data.concat(self.currentData)
                self.merged_data = self.merged_data.concat(self.forecastData)
            } else if (self.currentData != undefined && self.currentData instanceof Array && self.currentData.length > 0) {
                self.merged_data = self.merged_data.concat(self.currentData);
            } else if (self.forecastData != undefined && self.forecastData instanceof Array && self.forecastData.length > 0) {
                self.merged_data = self.merged_data.concat(self.forecastData);
            }
        };


        self.redraw = function () {
            w = $("#" + self.divID).width();
            h = $("#" + self.divID).height();
            self.width = w - self.boxing.left - self.boxing.right,
                    self.height = h - self.boxing.top - self.boxing.bottom;
            self.x = d3.time.scale()
                    .range([0, self.width]);
            self.y = d3.scale.linear()
                    .range([self.height, 0]);
            self.xAxis = d3.svg.axis()
                    .scale(self.x)
                    .ticks(d3.time.day, 1)
                    .tickFormat(d3.time.format('%b %d'))
                    .orient("bottom");
            self.yAxis = d3.svg.axis()
                    .scale(self.y)
                    .orient("left")
                    .tickSize(-self.width)
                    .ticks(5, 0)
                    .tickPadding(10);

            if (self.svg !== null) {
                self.svg.remove();
            }
            $("#" + self.divID).html("");
            self.svg = d3.select("#" + self.divID).append("svg")
                    .attr("width", self.width + self.boxing.left + self.boxing.right)
                    .attr("height", self.height + self.boxing.top + self.boxing.bottom)
                    .append("g")    
                    .attr("transform", "translate(" + self.boxing.left + "," + self.boxing.top + ")");


            var minDate = new Date(2999, 01, 01);
            self.merged_data.forEach(function (d) {
                if (d.date < minDate) {
                    minDate = d.date;
                }
            });

            var maxDate = new Date(0);
            self.merged_data.forEach(function (d) {
                if (d.date > maxDate) {
                    maxDate = d.date;
                }
            });

            var totalDateDiff = maxDate - minDate;
            totalDateDiff = totalDateDiff / (1000 * 60 * 60 * 24);

            if (w < 500 && !isNaN(totalDateDiff)) {
                if (totalDateDiff < 30) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.days, 7)
                            .tickFormat(d3.time.format('%b %d'))
                            .orient("bottom");
                } else if (totalDateDiff > 30 && totalDateDiff < 150) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.months)
                            .tickFormat(d3.time.format('%b %y'))
                            .orient("bottom");
                } else if (totalDateDiff > 150 && totalDateDiff < 365) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.years)
                            .tickFormat(d3.time.format('%b %y'))
                            .orient("bottom");
                } else if (totalDateDiff > 365) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.years)
                            .tickFormat(d3.time.format('%Y'))
                            .orient("bottom");
                }
            } else if (w > 500 && !isNaN(totalDateDiff)) {
                if (totalDateDiff < 15) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.days, 1)
                            .tickFormat(d3.time.format('%b %d'))
                            .orient("bottom");

                } else if (totalDateDiff > 15 && totalDateDiff <= 30) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.days, 4)
                            .tickFormat(d3.time.format('%b %d'))
                            .orient("bottom");

                } else if (totalDateDiff > 30 && totalDateDiff < 100) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.months, 1)
                            .tickFormat(d3.time.format('%b %y'))
                            .orient("bottom");
                } else if (totalDateDiff > 100 && totalDateDiff < 365) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.months)
                            .tickFormat(d3.time.format('%b %y'))
                            .orient("bottom");
                } else if (totalDateDiff > 365) {
                    self.xAxis = d3.svg.axis()
                            .scale(self.x)
                            .ticks(d3.time.years)
                            .tickFormat(d3.time.format('%b %Y'))
                            .orient("bottom");
                }
            } else if (isNaN(w) || isNaN(totalDateDiff)) {
                self.xAxis = d3.svg.axis()
                        .scale(self.x)
                        .ticks(d3.time.years)
                        .tickFormat(d3.time.format('%Y'))
                        .orient("bottom");
            }


            self.x.domain(d3.extent(self.merged_data, function (d) {
                return d.date;
            }));

            self.y.domain([d3.min(self.merged_data, function (d) {
                    return d.temperature_min - 10;
                }), d3.max(self.merged_data, function (d) {
                    return d.temperature_max + 10;
                })]);

            self.svg.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + self.height + ")")
                    .call(self.xAxis.orient("bottom"));


            self.svg.append("text")
                    .attr("x", 0)
                    .attr("y", self.height)
                    .style("text-anchor", "middle")
                    .text("");

            self.svg.append("g")
                    .attr("class", "y axis")
                    .call(self.yAxis)
                    .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("x", -85)
                    .attr("y", 6)
                    .attr("dy", ".71em")
                    .style("text-align", "middle")
                    .text("Temperature(°C)");

            self.svg.append("path")
                    .datum(self.averagesData)
                    .attr("class", self.areaClass)
                    .attr("d", self.area_characteristics_temperature);

            if (self.currentEnabled === true && self.currentData instanceof Array && self.currentData.length > 0) {
                self.svg.append("path")
                        .datum(self.currentData)
                        .attr("class", "line_characteristics_current_temperature_max")
                        .attr("d", self.line_characteristics_temperature_max);

                self.svg.append("path")
                        .datum(self.currentData)
                        .attr("class", "line_characteristics_current_temperature_min")
                        .attr("d", self.line_characteristics_temperature_min);
            }

            if (self.forecastEnabled === true && self.forecastData instanceof Array && self.forecastData.length > 0) {
                self.svg.append("path")
                        .datum(self.forecastData)
                        .attr("class", "line_characteristics_forecast_temperature_max")
                        .attr("d", self.line_characteristics_temperature_max)
                        .style("stroke-dasharray", "2.5,7");
                self.svg.append("path")
                        .datum(self.forecastData)
                        .attr("class", "line_characteristics_forecast_temperature_min")
                        .attr("d", self.line_characteristics_temperature_min)
                        .style("stroke-dasharray", "2.5,7");
            }
        }


        self.showCurrent = function (show)
        {
            self.currentEnabled = show;
        };

        self.showForecast = function (show)
        {
            self.forecastEnabled = show;
        };

        self.showAverages = function (show)
        {
            self.averagesEnabled = show;
        };
    }

1 个答案:

答案 0 :(得分:0)

应用CSS,如下所示。

.axis path, .axis line{
    stroke: navy;
    fill: none;
}
.y.axis path{
    stroke: brown;
    fill: none;
}
.y.axis line{
    stroke: grey;
    opacity: 0.5;
}