1 2 3 4 5 6 7 8 9 10 11 | <stocks> <stock> <day high="35.13" low="32" close="33.75" /> <day high="32.25" low="28.75" close="31.75" /> <day high="29" low="28.5" close="28.87" /> <day high="29.25" low="28.75" close="28.75" /> <day high="29.5" low="28.5" close="29.25" /> <day high="30.25" low="29" close="29.25" /> ... </stock> </stocks> |
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 | <?php class Day { var $low; var $high; var $close; public function __construct( $low, $high, $close ) { $this->low = $low; $this->high = $high; $this->close = $close; } } class Trace { var $days = array(); var $high = 0.0; var $low = 100000.0; public function addDay( $low, $high, $close ) { $this->days []= new Day( $low, $high, $close ); if ( $low < $this->low ) $this->low = $low; if ( $high > $this->high ) $this->high = $high; } public function hiloPath( $trans ) { $p = new Path(); $d = 0; foreach( $this->days as $day ) { $x = $trans->xscale( $d ); $y = $trans->yscale( $day->low ); $p->add( $x, $y ); $d += 1; } for( $d = (count( $this->days ) - 1); $d >= 0; $d -= 1 ) { $x = $trans->xscale( $d ); $y = $trans->yscale( $this->days[$d]->high ); $p->add( $x, $y ); } return $p; } public function closePath( $trans ) { $p = new Path(); $d = 0; foreach( $this->days as $day ) { $x = $trans->xscale( $d ); $y = $trans->yscale( $day->close ); $p->add( $x, $y ); $d += 1; } return $p; } } class Data { var $traces = array(); var $high = 0; var $low = 100000; function parseXML( $file ) { $data_dom = new DomDocument(); $data_dom->load( $file ); $elStocks = $data_dom->getElementsByTagName( 'stock' ); foreach( $elStocks as $stock ) { $trace = new Trace(); $days = $stock->getElementsByTagName( 'day' ); foreach( $days as $day ) { $trace->addDay( (float)$day->getAttribute('low'), (float)$day->getAttribute('high'), (float)$day->getAttribute('close') ); } $this->traces []= $trace; if ( $trace->high > $this->high ) $this->high = $trace->high; if ( $trace->low < $this->low ) $this->low = $trace->low; } } } ?> |
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 | <?php interface ITransform { function xscale( $x ); function yscale( $x ); } class Transform implements ITransform { protected $ox; protected $oy; protected $xscale; protected $yscale; protected $xoffset = 0; protected $yoffset = 0; public function xscale( $x ) { return $this->ox + ( ( $x - $this->xoffset ) * $this->xscale ); } public function yscale( $y ) { return $this->oy - ( ( $y - $this->yoffset ) * $this->yscale ); } } class Point { var $x; var $y; public function __construct( $x, $y ) { $this->x = $x; $this->y = $y; } } class Path { private $points = array(); public function add( $x, $y ) { $this->points []= new Point( $x, $y ); } public function toSVG() { $svg = ""; $svg .= "M ".$this->points[0]->x." ".$this->points[0]->y." "; foreach( $this->points as $pt ) { $svg .= "L ".$pt->x." ".$pt->y." "; } return $svg; } } ?> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |