IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques & Frameworks Discussion :

[D3js] Histogrammes décalés


Sujet :

Bibliothèques & Frameworks

  1. #1
    Membre du Club
    Homme Profil pro
    Lycéen
    Inscrit en
    Avril 2014
    Messages
    123
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Avril 2014
    Messages : 123
    Points : 66
    Points
    66
    Par défaut [D3js] Histogrammes décalés
    Bonjour j'ai fait un graphique avec la librairie d3, mais mes histogrammes sont décalés par rapport aux axes verticaux, et la ligne tout à gauche est trop grande, est-ce que vous pourriez m'aider à corriger ça svp ? Merci !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
     
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
     
        <title>Positive and Negative Value Bar Chart</title>
     
     
        <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
       <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
       <style>
       body{
    	font-family: sans-serif;
    }
    .positive{
    		fill: lightsteelblue;
    		fill-opacity: 0.9;
    	}
    .negative{
    		fill: darkred;
    		fill-opacity: 0.9;
    	}
    .axis path,
    .axis line {
    			fill: none;
    			stroke: black;
    			shape-rendering: crispEdges;
    	}
    .axis text {
    			font-family: sans-serif;
    			font-size: 10px;	
    	}
    div.tooltip {   
    	  position: absolute;     
    	  text-align: center;
    	  vertical-align: middle;   
    	  width: 150px;                  
    	  height: 50px;                 
    	  padding: 5px; 
    	  color: #000000;          
    	  font-size: 12px;     
    	  background: #FFF;
    	  box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
    	  border: 2px;  
    	  border-radius: 8px;  
    	  pointer-events: none;                  
    	} 
    li{
    	overflow:hidden;
    	display: inline-block;
    }
    ul{
    	padding-left: 0;
    	padding-right: 0;
    }
    #chart-container{
    	float:left;
    }
       </style>
      </head>
     
      <body>
      <div class ="description">
                <table>
              <tr>
                <td>
                  <select>
                    <option id="item" value="demand">Demand Ride Percent Change</option>
                    <option id="item" value="bus">Bus Ride Percent Change</option>
                  </select>
                </td>
            </tr>
            <tr>
                  <td>
                    <ul>
                      <li><input type="checkbox" class="checkbox" name="check" value="sort"> </li>
                      <li>Sort by percent change</li>
                    </ul>
                  </div>
                </td>
     
            </tr>
              </table>
          </div>
          <br>
          <div id="chart-container"></div>
           <script>
    	   var dataset;
     
    //Define bar chart function 
    	function barChart(dataset){	
     
    		//Set width and height as fixed variables
    		var w = 1000;
    		var h = 1000;
    		var padding = 40;
    		//Scale function for axes and radius
    		var yScale = d3.scale.linear()
    						.domain(d3.extent(dataset, function(d){return d.lundi;}))
    						//.range([w+padding,padding]);
    						//.range([d3.min(dataset, function(d) { return d.lundi;}),d3.max(dataset, function(d) { return d.lundi;})]);
    	                    .range([500+padding,padding]); //pourquoi dansz ce sens ?
    						//.range([h,0]);
    		/*var xScale = d3.scale.ordinal()
    						.domain(dataset.map(function(d){ return d.horaire;}))
    						.rangeRoundBands([padding,w]); //poruquoi round ? pourquoi xCale et axisScale ? */
     
     
     
    		//To format axis as a percent
    		//var formatPercent = d3.format("%1");
     
    		//Create y axis
    		var yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(10);
     
     
    		var axisScale =  d3.scale.ordinal()
    						.domain(dataset.map(function(d){ return d.horaire;}))
    						//.rangeRoundBands([padding,w],0.15); //pourquoi intervalle de 0.5  et round ?
    						.rangeRoundBands([padding,w])
    						//.rangePoints([padding,w]);
            var xAxis = d3.svg.axis().scale(axisScale)/*.tickValues(10)*/.tickValues(axisScale.domain().filter(function(d, i) { return !(i % 4); }));
     
     
     
     
    		//Define key function
    		var key = function(d){return d.horaire};
     
    		//Define tooltip for hover-over info windows
    		var div = d3.select("body").append("div")   
      							.attr("class", "tooltip")               
      							.style("opacity", 0);
     
    		//Create svg element
    		var svg = d3.select("#chart-container").append("svg")
    				.attr("width", w).attr("height", h)
    				.attr("id", "chart")
    				.attr("viewBox", "0 0 "+w+ " "+h) // pour le responsive
    				.attr("preserveAspectRatio", "xMinYMin");
    		//console.log("TARGET   ");
    		//Resizing function to maintain aspect ratio (uses jquery)
    		//var targetWidth2 = $("body").width();
    		var aspect = w / h;
    		var chart = $("#chart");
    			$(window).on("resize", function() {
    			    var targetWidth = $("body").width();
    			   	//console.log("TARGET   "+targetWidth2);
     
    	    		if(targetWidth<w){
    	    			chart.attr("width", targetWidth); 
    	    			chart.attr("height", targetWidth / aspect); 			
    	    		}
    	    		else{
     
    	    			chart.attr("width", w);  
    	    			chart.attr("height", w / aspect);	//pourquoi ?
    	    		}
     
    			});
     
     
    		//Initialize state of chart according to drop down menu
    		var state = d3.selectAll("option");
     
    		//Create barchart
    		svg.selectAll("rect")
    			.data(dataset, key)
    			.enter()
    		  	.append("rect")
    		    .attr("class", function(d){return d.lundi < 0 ? "negative" : "positive";})
    		    .attr({
    		    	x: function(d){
    					//console.log("X :"+xScale(d.horaire));
    		    		return axisScale(d.horaire);
    		    	},
     
     
    		    	y: function(d){
    					console.log("Y :"+yScale(Math.max(0, d.lundi)));
    		    		return yScale(Math.max(0, d.lundi));  
    		    	},
    		    	width: axisScale.rangeBand(),
    		    	height: function(d){
    		    		return Math.abs(yScale(d.lundi) - yScale(0)); 	//pourquoi ?
    		    	}
    		    })
    		    .on('mouseover', function(d){
    							d3.select(this)
    							    .style("opacity", 0.2)
    							    .style("stroke", "black")
     
    					var info = div
    							    .style("opacity", 1)
    							    .style("left", (d3.event.pageX+10) + "px")
    							    .style("top", (d3.event.pageY-30) + "px")
    							    .text(d.horaire);
     
    					if(state[0][0].selected){
    						info.append("p")
    							    .text(/*formatPercent(*/d.lundi/*)*/);
     
    					}
    					else if(state[0][1].selected){
    						info.append("p")
    							    .text(formatPercent(d.horaire));
    					}
     
     
     
    						})
            				.on('mouseout', function(d){
            					d3.select(this)
    							.style({'stroke-opacity':0.5,'stroke':'#a8a8a8'})
    							.style("opacity",1);
     
    							div
    	    						.style("opacity", 0);
            				});
     
    		//Add y-axis
    		svg.append("g")
    				.attr("class", "y axis")
    				.attr("transform", "translate(40,0)") //padding = 40
    				.call(yAxis);
     
    		svg.append("g")
    				.attr("class", "x axis")
    				.attr("transform", "translate(0,0)")
    				/*.selectAll('path')
    				.style({fill: 'none', stroke: "#3c763d"})
    				.selectAll('line')
    				.style({stroke: "#3c763d"})*/
    				.call(xAxis);
     
    		//svg.append("g")
    			//	.attr("width", 100)
                  //  .attr("height", 0)
    				//.call(xAxis);	
     
    		svg.selectAll("line.horizontalGrid").data(yScale.ticks(10)).enter()
    		.append("line")
            .attr(
            {
                "class":"horizontalGrid",
                "x1" : padding,
                "x2" : w,
                "y1" : function(d){ return yScale(d);},
                "y2" : function(d){ return yScale(d);},
                "fill" : "none",
                "shape-rendering" : "crispEdges",
                "stroke" : "black",
                "stroke-width" : "1px"
            });
    console.log('test' + axisScale(dataset[0].horaire));
    		/*svg.selectAll("g").append("line.verticalGrid").data("08:00").append("line").attr(
            {
                "class":"verticalGrid",
                "x1" : axisScale(dataset[0].horaire),
                "x2" : axisScale(dataset[0].horaire),
                "y1" : 0,
                "y2" : h,
                "fill" : "none",
                "shape-rendering" : "crispEdges",
                "stroke" : "black",
                "stroke-width" : "1px"
            });*/
     
    /*svg.selectAll("line.verticalGrid").data(axisScale.ticks(axisScale.domain().filter(function(d, i) { return !(i % 4)}))).enter()
        .append("line")
            .attr(
            {
                "class":"verticalGrid",
                "x1" : function(d){ return axisScale(d);},
                "x2" : function(d){ return axisScale(d);},
                "y1" : 0,
                "y2" : h,
                "fill" : "none",
                "shape-rendering" : "crispEdges",
                "stroke" : "black",
                "stroke-width" : "1px"
            });*/
     
            svg.selectAll("g").append("line")
           .attr("x1",axisScale(dataset[0].horaire- padding) )  //<<== change your code here
           .attr("y1", 0)
           .attr("x2", axisScale(dataset[0].horaire - padding))  //<<== and here
           .attr("y2", 500 )
    		//Sort data when sort is checked
    		d3.selectAll(".checkbox").
    		on("change", function(){
    			console.log("sss");
    			var x0 = axisScale.domain(dataset.sort(sortChoice())
    			.map(function(d){return d.horaire}))
    			.copy();
     
    			var transition = svg.transition().duration(750);
    			var delay = function(d, i){return i*10;};
     
    			transition.selectAll("rect")
    			.delay(delay)
    			.attr("x", function(d){return x0(d.horaire);});
     
    		})
     
    		//Function to sort data when sort box is checked
    		function sortChoice(){
    				var state = d3.selectAll("option");
    				var sort = d3.selectAll(".checkbox");
     
    				if(sort[0][0].checked && state[0][0].selected){
    				    console.log("begin");
    					var out = function(a,b){
    						return b.lundi - a.lundi; // ?
    						}
    						//console.log(out);
    					return out;
    				}
    				/*else if(sort[0][0].checked && state[0][1].selected){
    					var out = function(a,b){return b.bus_change - a.bus_change;}
    					return out;
    				}*/
    				else{
    				     console.log("return");
    					var out = function(a,b){return d3.ascending(a.horaire, b.horaire);}
    					return out;
     
    				}
    		};
     
    		//Change data to correct values on input change
    		//thid for second choice
    		/*
    			d3.selectAll("select").
    			on("change", function() {
     
    				var value= this.value;
    				console.log("val: "+value);
     
    				if(value=="bus"){
    					var x_value = function(d){return d.bus_change;};
    					var color = function(d){return d.bus_change < 0 ? "negative" : "positive";};
    					var y_value = function(d){
    			    		return yScale(Math.max(0, d.bus_change)); 
    			    	};
    			    	var height_value = function(d){
    			    		return Math.abs(yScale(d.bus_change) - yScale(0));
    			    	};	
    				}
    				else if(value=="demand"){
    					var x_value = function(d){return d.lundi;};
    					var color = function(d){return d.lundi < 0 ? "negative" : "positive";};
    					var y_value = function(d){
    			    		return yScale(Math.max(0, d.lundi)); 
    			    	};
    			    	var height_value = function(d){
    			    		return Math.abs(yScale(d.lundi) - yScale(0)); 
    			    	};	
    				}
     
    				//Update y scale
    				yScale.domain(d3.extent(dataset, x_value));
     
    				//Update with correct data
    				var rect = svg.selectAll("rect").data(dataset, key);
    				rect.exit().remove();
     
    				//Transition chart to new data
    				rect
    				.transition()
    				.duration(2000)
    				.ease("linear")
    				.each("start", function(){
    					d3.select(this)
    					.attr("width", "0.2")
    					.attr("class", color)
    				})
    				.attr({
    			    	x: function(d){
    			    		return xScale(d.horaire);
    			    	},
    			    	y: y_value,
    			    	width: xScale.rangeBand(),
    			    	height: height_value
     
    				});
     
    				//Update y-axis
    				svg.select(".y.axis")
    					.transition()
    					.duration(1000)
    					.ease("linear")
    					.call(yAxis);
    			});*/
     
    	};
     
    	//Load data and call bar chart function 
    		d3.csv("transportation_change_2012-2013_small.csv", function(error,data){
    				if(error){
    					console.log(error);
    				}
    				else{
    					data.forEach(function(d) {
    						d.lundi = parseFloat(d.lundi);
     
    					});
    					console.log(data.length); // ?
    					dataset=data;
    					barChart(dataset);
    				}
    			});
     
    	   </script>
        </div>
      </body>
      </html>
    Fichier csv :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
     
    horaire,lundi,mardi,mercredi,jeudi,vendredi,samedi,dimanche
    00:00,4,0,0,0,0,0,0
    00:15,0,0,0,0,0,0,0
    00:30,0,0,0,0,0,0,0
    00:45,0,0,0,0,0,0,0
    01:00,0,0,0,0,0,0,0
    01:15,0,0,0,0,0,0,0
    01:30,0,0,0,0,0,0,0
    01:45,0,0,0,0,0,0,0
    02:00,0,0,0,0,0,0,0
    02:15,0,0,0,0,0,0,0
    02:30,0,0,0,0,0,0,0
    02:45,0,0,0,0,0,0,0
    03:00,0,0,0,0,0,0,0
    03:15,0,0,0,0,0,0,0
    03:30,0,0,0,0,0,0,0
    03:45,0,0,0,0,0,0,0
    04:00,0,0,0,0,0,0,0
    04:15,0,0,0,0,0,0,0
    04:30,0,0,0,0,0,0,0
    04:45,0,0,0,0,0,0,0
    05:00,0,0,0,0,0,0,0
    05:15,0,0,0,0,0,0,0
    05:30,0,0,0,0,0,0,0
    05:45,0,0,0,0,0,0,0
    06:00,0,0,0,0,0,0,0
    06:15,0,0,0,0,0,0,0
    06:30,0,0,0,0,0,0,0
    06:45,0,0,0,0,0,0,0
    07:00,0,0,0,0,0,0,0
    07:15,0,0,0,0,0,0,0
    07:30,0,0,0,0,0,0,0
    07:45,0,0,0,0,0,0,0
    08:00,1,1,1,1,1,1,0
    08:15,1,1,1,1,1,1,0
    08:30,6,5,7,3,5,4,0
    08:45,5,5,6,4,6,5,0
    09:00,52,50,47,55,53,38,0
    09:15,56,51,62,57,54,41,0
    09:30,60,64,67,64,57,54,0
    09:45,85,72,77,69,63,64,0
    10:00,80,87,74,82,71,75,14
    10:15,80,90,87,87,84,77,14
    10:30,80,85,85,92,83,78,14
    10:45,83,87,90,89,83,79,14
    11:00,86,89,78,81,84,81,14
    11:15,89,83,77,79,80,83,14
    11:30,89,84,74,83,78,77,14
    11:45,88,92,76,81,75,72,14
    12:00,78,83,62,77,69,63,13
    12:15,77,64,54,63,55,52,13
    12:30,61,58,52,55,48,52,12
    12:45,58,53,48,50,45,43,12
    13:00,58,57,45,50,41,44,12
    13:15,58,57,45,45,45,40,12
    13:30,55,57,42,47,49,42,12
    13:45,52,61,43,56,53,48,12
    14:00,71,64,59,70,68,49,13
    14:15,71,72,72,83,71,62,14
    14:30,81,73,87,83,76,72,14
    14:45,77,90,90,86,76,77,14
    15:00,82,95,90,91,74,74,14
    15:15,86,96,92,77,79,72,14
    15:30,89,92,88,78,83,72,14
    15:45,93,90,82,83,78,74,14
    16:00,101,87,85,80,79,78,14
    16:15,100,90,89,84,83,77,14
    16:30,98,92,89,82,83,78,14
    16:45,88,86,91,83,86,72,14
    17:00,90,89,91,81,80,66,14
    17:15,87,90,86,73,81,61,14
    17:30,86,90,86,77,73,59,14
    17:45,88,93,91,64,70,52,14
    18:00,78,83,68,65,56,33,11
    18:15,74,77,67,67,54,30,11
    18:30,58,54,51,49,44,25,11
    18:45,47,50,50,44,35,23,11
    19:00,12,17,14,12,17,11,11
    19:15,12,11,15,11,13,10,11
    19:30,9,7,11,9,8,7,11
    19:45,9,7,11,9,9,7,11
    20:00,4,4,3,4,4,3,5
    20:15,4,4,3,4,4,3,5
    20:30,4,4,3,4,4,3,5
    20:45,4,4,3,4,4,3,5
    21:00,4,4,3,4,4,3,0
    21:15,4,4,3,4,4,3,0
    21:30,1,1,1,1,1,1,0
    21:45,1,1,1,1,1,1,0
    22:00,0,0,0,0,0,0,0
    22:15,0,0,0,0,0,0,0
    22:30,0,0,0,0,0,0,0
    22:45,0,0,0,0,0,0,0
    23:00,0,0,0,0,0,0,0
    23:15,0,0,0,0,0,0,0
    23:30,0,0,0,0,0,0,0
    23:45,4,0,0,0,0,0,0

  2. #2
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    16 946
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 16 946
    Points : 44 086
    Points
    44 086
    Par défaut
    Bonjour,
    j'ai repérer au moins deux choses

    • pourquoi 2 fois le padding, cela fait que la ligne de base n'est pas sur l'axe des X.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    .range([500+padding,padding]); //pourquoi dansz ce sens ?
    • pourquoi mettre un var h = 1000 alors que ton graphe ne fait que 500 de haut.

    mes histogrammes sont décalés par rapport aux axes verticaux
    là je ne vois rien d'anormal, les barres sont centrées sur les lignes verticales des heures.

Discussions similaires

  1. Réponses: 3
    Dernier message: 27/01/2005, 10h53
  2. [CR8.5] Graphique : comment mélanger histogramme et ligne ?
    Par kiki26 dans le forum SAP Crystal Reports
    Réponses: 4
    Dernier message: 18/01/2005, 15h28
  3. TChart et histogramme
    Par coucoucmoi dans le forum Composants VCL
    Réponses: 8
    Dernier message: 30/08/2004, 16h10
  4. Questions sur chartfx (histogramme)
    Par lejert dans le forum C++Builder
    Réponses: 2
    Dernier message: 20/01/2004, 13h54
  5. [TChart] comment remplir un histogramme avec du rouge strié.
    Par :GREG: dans le forum Composants VCL
    Réponses: 2
    Dernier message: 12/08/2002, 09h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo