View Full Version : Bar chart with yui-ext
rodiniz
01-23-2007, 05:52 AM
This is justa a sample showing how extensible is yui-ext.
http://www.rodrigodiniz.qsh.eu/Barchart.aspx
Please feel free to make sugestions... at my blog please.
These sample its not working ..is stil being upgraded to EXT 1.0.
Animal
01-23-2007, 06:05 AM
Nice, simple concept.
Wouldn't an easier way of generating a random color be:
"#" + Math.floor(Math.random() * 0xFFFFFF).toString(16);
:?:
Animal
01-23-2007, 06:16 AM
But your Legend box is not simple. It's tortured! Why have explicit positioning and sizing? Just use a div container with default, automatic sizing, and use naturally flowed divs inside for the legend, spaced out with some margins.
Perhaps something along the lines of (Off the top of my head, and totally untried.....):
<style>
.Legend {
border:1px solid black;
line-height:2em;
}
.color-key {
float:left:
height:20px;
width:20px;
margin-right:20px;
clear:left;
}
</style>
<div class="Legend">
Legend text 1<div class="color-key" style="color:"></div>
Legend text 2<div class="color-key" style="color:"></div>
Legend text 2<div class="color-key" style="color:"></div>
</div>
Animal
01-23-2007, 06:21 AM
Same goes for your chart really. The only explicit styles that need to be set are the width and color. The divs can flow naturally and don't have to be explicitly positioned with calculations! Just arrange a CSS class to give them margins, and float the coloured bar, and the %age text at opposite sides.
rodiniz
01-23-2007, 08:35 AM
Well...Like I said on my site...this is just the begining..
You are free to take the code and improve it...
rodiniz
02-08-2007, 08:02 AM
The sample was updated to use the Vector Graphics Library posted here...
It doesnt work in Firefox..but its a matter of time.
rodiniz
02-16-2007, 06:56 AM
The sample was updated and now works in firefox.
Thanks to Michael for sharing the code.
willgillen
03-14-2007, 02:33 PM
I see "Ext.SVG is null or not an object" when I try to load the page... something missing?
rodiniz
03-14-2007, 03:57 PM
I see "Ext.SVG is null or not an object" when I try to load the page... something missing?
Sorry ..I still have to update this sample to Ext alpha.
And I am having some issues..that the previous version didn't have.
jgarcia@tdg-i.com
03-21-2007, 08:45 AM
If you have publicly linked examples, you should iron out the issues on a differnt page. I went to your site thinking i would see something but i got 3 critical errors and a dead page.
rodiniz
03-21-2007, 10:14 AM
If you have publicly linked examples, you should iron out the issues on a differnt page. I went to your site thinking i would see something but i got 3 critical errors and a dead page.
If I could I would remove this topic until I had time to fix the issues..but I do not have that power.
This sample was working at the time I made the fisrt post.
jgarcia@tdg-i.com
03-21-2007, 08:08 PM
Understood. :)
A suggestion for the future: You can edit your posts. ;) If you have examples for folks to reference, consider that "production". It sucks to go to someone's site expecting to see something working and then have it blow up.
:wink:
Animal
04-07-2007, 06:34 PM
Here's a very simple bar chart. Mostly just CSS based with a little bit of javascript to handle the legend going over the end of a bar.
This bit was inspired by a question from another poster this morning asking about how t make text in a coloured bar more visible if it went out the end of a bar.
Usage should be obvious from the example. All that needs really figuring out properly is a series of colour values for the classes x-chart-color-0 to x-chart-color-20 (20 seems a good cutoff point, any more than that would just be confusing to a viewer)
It drops into the examples/dialog directory
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Bar Chart Dialog Example</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
<!-- LIBS -->
<script type="text/javascript" src="../../yui-utilities.js"></script>
<script type="text/javascript" src="../../ext-yui-adapter.js"></script>
<!-- ENDLIBS -->
<script type="text/javascript" src="../../ext-all-debug.js"></script>
<style type="text/css">
.x-chart-bar {
height: 1.8em;
margin-bottom: 5px;
overflow: hidden;
white-space: nowrap;
padding-top:.4em;
padding-left:.4em;
float:left
}
.x-chart-color-0 {
background-color: #229999;
}
.x-chart-color-1 {
background-color: #226699;
}
.x-chart-color-2 {
background-color: #88aa99;
}
.x-chart-color-3 {
background-color: #229911;
}
.x-chart-color-4 {
background-color: #992244;
}
.x-chart-color-5 {
background-color: #999922;
}
.x-chart-legend-1 {
color: white
}
.x-chart-legend-2 {
color: black,
position:relative
}
</style>
<script language="javascript">
/*
* Ext - JS Library 1.0 Alpha 3 - Rev 1
* Copyright(c) 2006-2007, Jack Slocum.
*/
// create the HelloWorld application (single instance)
var HelloWorld = function(){
// everything in this space is private and only accessible in the HelloWorld block
// define some private variables
var dialog, showBtn;
// return a public interface
return {
init : function(){
showBtn = Ext.get('show-dialog-btn');
// attach to click event
showBtn.on('click', this.showDialog, this);
},
showDialog : function(){
if(!dialog){ // lazy initialize the dialog and only create it once
dialog = new Ext.BasicDialog("hello-dlg", {
width:500,
height:300,
shadow:true,
minWidth:300,
minHeight:250,
proxyDrag: true
});
dialog.addKeyListener(27, dialog.hide, dialog);
dialog.addButton('Submit', dialog.hide, dialog).disable();
dialog.addButton('Close', dialog.hide, dialog);
}
dialog.show();
var c = new Chart(dialog.body, {
total: 100,
values: [34, 67, 2.25, 6, 8.1, 34],
legend: ["Bar one", "Bar Two", "Bar Three", "Bar Four", "Bar Five", "Bar Six"]
});
c.render();
dialog.on("resize", c.relabel, c);
}
};
}();
/**
@param container The container to put the chart in
@param configuration
@cfg total {Number} The value of which the other values are to be rendered as %ages
@cfg values {Array} The values for each bar
@cfg legend {Optional Arra]} The legend to go inside each bar
*/
Chart = function(container, config) {
this.el = Ext.get(container);
this.el.setStyle({padding:"20px"});
Ext.apply(this, config);
}
Chart.prototype = {
render: function() {
var dh = Ext.DomHelper;
this.bar = [];
this.label = [];
this.remainder = []
for (var i = 0; i < this.values.length; i++) {
var pct = this.values[i]/this.total*100;
this.bar.push(dh.append(this.el, {
cls: 'x-chart-bar x-chart-color-' + i,
style: {
clear: "left",
width: pct + "%"
}
}, true));
var legend = this.legend ? this.legend[i] : (pct + '%');
this.label[i] = dh.append(this.bar[i], {
tag: 'span',
cls: 'x-chart-legend-1',
html: legend
}, true);
var barWidth = this.bar[i].getBox().width;
var labelWidth = this.label[i].getBox().width;
if (labelWidth > barWidth) {
this.remainder[i] = remainder = dh.append(this.el, {
cls: 'x-chart-bar',
style: {
position: 'relative',
left: (-barWidth) + "px",
"z-index": -1
}
}, true);
dh.append(this.remainder[i], {
tag: 'span',
cls: 'x-chart-legend-2',
html: legend,
}, true);
}
}
},
relabel: function() {
for (var i = 0; i < this.values.length; i++) {
var barWidth = this.bar[i].getBox().width;
var labelWidth = this.label[i].getBox().width;
if (labelWidth > barWidth) {
this.remainder[i].setStyle({
left: (-barWidth) + "px",
});
}
}
}
};
// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
Ext.onReady(HelloWorld.showDialog, HelloWorld, true);
</script>
<!-- Common Styles for the examples -->
<link rel="stylesheet" type="text/css" href="../examples.css" />
</head>
<body>
<h1>Bar Chart Dialog</h1>
<!-- The dialog is created from existing markup.
The inline styles just hide it until it created and should be in a stylesheet -->
<div id="hello-dlg" style="visibility:hidden;position:absolute;top:0px;">
<div class="x-dlg-hd">Chart Dialog</div>
<div class="x-dlg-bd"></div>
</div>
</body>
</html>
Wolfgang
04-18-2007, 04:44 PM
Looks good - maybe we get at chart widget in the future ;)
Sigurd
04-20-2007, 03:09 AM
I alsow spet several days to wirte bar charts on YU-EXT
Code is prety ugly and need to be ported to Ext. Alosow it needs some autogeneration improvements :)
Here is the demo (http://phpvoid.com/demos/charts/demo.html)
Alsow there are some options to tune how bars looks like and generation options. All these can be found in sources. No docs for now ;)
Wolfgang
04-20-2007, 05:45 AM
Wow - this one looks really good, too :)
mophisoft
06-18-2007, 08:59 AM
Good! I sought it for a long time. But Animal's codes seems cannot work in ie, and how to achieve Sigurd's cool demo?
JorisA
06-18-2007, 09:54 AM
looks really great.
Maybe you could add it to the wiki (http://extjs.com/learn/Ext_Extensions)
jgarcia@tdg-i.com
06-18-2007, 01:53 PM
how is this related to extjs?
JorisA
06-18-2007, 06:32 PM
Ah I see....
didn't look at the source closely enough :(
n/m
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.