function stacked($data, $title, $filename) { // Dataset definition $DataSet = new pData(); $values = $data['values']; $bar = new pChart(1040, 230); for ($i = 0; $i < count($values); $i++) { $DataSet->AddPoint($values[$i], "Serie" . ($i + 1)); if ($i != 0 && $i != count($values) - 1) { $DataSet->AddSerie("Serie" . ($i + 1)); } } $DataSet->SetAbsciseLabelSerie("Serie1"); $DataSet->SetXAxisFormat("date"); $DataSet->SetYAxisFormat($data['yformat']); // Initialise the graph $bar->setDateFormat("M.d"); $bar->setFontProperties("Fonts/consola.ttf", 8); $bar->setGraphArea(80, 30, 1020, 200); $bar->drawFilledRoundedRectangle(2, 2, 1037, 227, 5, 240, 240, 240); $bar->drawRoundedRectangle(0, 0, 1039, 229, 5, 230, 230, 230); $bar->loadColorPalette('chartcolors.txt', ','); $bar->drawGraphArea(255, 255, 255, TRUE); $bar->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_ADDALL, 150, 150, 150, TRUE, 0, 2, TRUE); $bar->drawGrid(4, TRUE, 230, 230, 230, 50); // Draw the 0 line $bar->setFontProperties("Fonts/consola.ttf", 6); $bar->drawTreshold(0, 143, 55, 72, TRUE, TRUE); // Draw the bar graph $bar->drawStackedBarGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 100); $bar->setFontProperties("Fonts/tahoma.ttf", 8); $bar->writeValues($DataSet->GetData(), $DataSet->GetDataDescription(), array("Serie" . count($values))); // Finish the graph $bar->setFontProperties("Fonts/MankSans.ttf", 10); $bar->drawTitle(10, 20, $title, 100, 100, 100); $bar->Render($filename); }
*/ // Standard inclusions include "pChart/pData.class"; include "pChart/pChart.class"; // Dataset definition $DataSet = new pData(); $DataSet->AddPoint(array(100, 320, 200, 10, 43), "Serie1"); $DataSet->AddPoint(array(23, 432, 43, 153, 234), "Serie2"); $DataSet->AddPoint(array(1217541600, 1217628000, 1217714400, 1217800800, 1217887200), "Serie3"); $DataSet->AddSerie("Serie1"); $DataSet->AddSerie("Serie2"); $DataSet->SetAbsciseLabelSerie("Serie3"); $DataSet->SetSerieName("Incoming", "Serie1"); $DataSet->SetSerieName("Outgoing", "Serie2"); $DataSet->SetYAxisName("Call duration"); $DataSet->SetYAxisFormat("time"); $DataSet->SetXAxisFormat("date"); // Initialise the graph $Test = new pChart(700, 230); $Test->setFontProperties("Fonts/tahoma.ttf", 8); $Test->setGraphArea(85, 30, 650, 200); $Test->drawFilledRoundedRectangle(7, 7, 693, 223, 5, 240, 240, 240); $Test->drawRoundedRectangle(5, 5, 695, 225, 5, 230, 230, 230); $Test->drawGraphArea(255, 255, 255, TRUE); $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2); $Test->drawGrid(4, TRUE, 230, 230, 230, 50); // Draw the 0 line $Test->setFontProperties("Fonts/tahoma.ttf", 6); $Test->drawTreshold(0, 143, 55, 72, TRUE, TRUE); // Draw the line graph $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription());
protected function generateGraphImage($finalData, $hourflag, $title = 'Sticky Charts', $x_axis_format = 'date') { $path = $this->config->item('csv_upload_path'); if (!empty($finalData['data'])) { $DataSet = new pData(); $in = 0; foreach ($finalData['data'] as $seriesData) { $in++; $seriesIndex = 'Serie' . $in; $DataSet->AddPoint($seriesData['data'], $seriesIndex); $DataSet->SetSerieName($seriesData['name'], $seriesIndex); $DataSet->AddSerie($seriesIndex); } $xAxisArray = array(); $in++; $seriesIndex = 'Serie' . $in; $catCount = count($finalData['cat']); if ($catCount <= 10) { $DataSet->SetXAxisFormat($x_axis_format); } foreach ($finalData['cat'] as $catD) { if ($catCount > 10) { $xAxisArray[] = ''; } else { $xAxisArray[] = strtotime($catD); } } $DataSet->SetYAxisFormat("number"); $DataSet->AddPoint($xAxisArray, $seriesIndex); $DataSet->SetAbsciseLabelSerie($seriesIndex); $DataSet->SetYAxisName($finalData['y_title']); $DataSet->SetXAxisName($finalData['x_title']); // Initialise the graph $Test = new pChart(985, 458); $Test->drawBackground(247, 226, 180); $Test->setFontProperties(APPPATH . '3rdparty/pchart/Fonts/tahoma.ttf', 8); $Test->setGraphArea(40, 30, 950, 400); $Test->drawGraphArea(109, 110, 114, false); $Test->drawGrid(4, false, 0, 0, 0, 50); $Test->setFontProperties(APPPATH . '3rdparty/pchart/Fonts/tahoma.ttf', 6); // Draw the line graph if ($title == 'Violation Report') { //!$hourflag && $sCount = count($finalData['data']); if ($sCount > 0) { for ($m = 0; $m < $sCount; $m++) { $color = Color_handler::get_next($m); $rgb = $color->get_rgb(); $Test->setColorPalette($m, $rgb['r'], $rgb['g'], $rgb['b']); } } $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_NORMAL, 0, 0, 0, TRUE, 0, 0, TRUE); $Test->drawBarGraph($DataSet->GetData(), $DataSet->GetDataDescription()); } else { $sCount = count($finalData['data']); if ($sCount > 0) { for ($m = 0; $m < $sCount; $m++) { $color = Color_handler::get_next($m % 3); $rgb = $color->get_rgb(); $Test->setColorPalette($m, $rgb['r'], $rgb['g'], $rgb['b']); } } $Test->setLineStyle(2); $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_START0, 0, 0, 0, TRUE, 0, 2); $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription()); $Test->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 2); } // Finish the graph $Test->setFontProperties(APPPATH . '3rdparty/pchart/Fonts/tahoma.ttf', 8); $Test->setFontProperties(APPPATH . '3rdparty/pchart/Fonts/tahoma.ttf', 10); $imgName = uniqid('graph_') . '.png'; $Test->Render($path . $imgName); return upload_to_amazon_graphImage($imgName, $path); } }
for ($i = 0; $i < count($data_server); $i++) { if ($i == 0) { $XAxis[] = date("H:i", $data_server[$i]); } else { $OLNum[] = $data_server[$i]; } } } $pChartDataSet = new pData(); $pChartDataSet->AddPoint($XAxis, "Serie1"); $pChartDataSet->AddPoint($OLNum, "Serie2"); $pChartDataSet->AddSerie("Serie2"); $pChartDataSet->RemoveSerie("Serie1"); $pChartDataSet->SetAbsciseLabelSerie("Serie1"); $pChartDataSet->SetYAxisName("在\n线\n人\n数\n//\n人"); $pChartDataSet->SetYAxisFormat("floor"); $pChartDataSet->SetXAxisFormat("number"); // 设置作图区域 $pChartGraph = new pChart($imgWidth, 253); $pChartGraph->drawGraphAreaGradient(90, 90, 90, 90, TARGET_BACKGROUND); $pChartGraph->setGraphArea(70, 30, $imgWidth + 70 - 90, 253 + 30 - 80); $pChartGraph->setFontProperties(DRAWFONE_PATH, 8); $pChartGraph->drawScale($pChartDataSet->GetData(), $pChartDataSet->GetDataDescription(), SCALE_NORMAL, 250, 250, 250, TRUE, 0, 0, FALSE, 1); // 开始作图 $pChartGraph->setColorPalette(0, 0, 255, 255); $pChartGraph->drawGraphAreaGradient(40, 40, 40, -50); $pChartGraph->drawGrid(1, TRUE, 115, 115, 115, 10); $pChartGraph->setShadowProperties(3, 3, 0, 0, 0, 30, 4); $pChartGraph->drawFilledLineGraph($pChartDataSet->GetData(), $pChartDataSet->GetDataDescription(), 25); $pChartGraph->clearShadow(); $pChartGraph->setFontProperties(DRAWFONE_PATH, 10);
function cartesianChart($type, $x, $y, $w, $h, $imgname = '', $config = array()) { $w -= 4; $h -= 4; $settings = array('FontName' => 'tahoma.ttf', 'FontSize' => 8, 'LegendFontSize' => 8, 'LegendFontName' => 'tahoma.ttf', 'Logo' => '', 'LogoTransparency' => 20, 'XAxisFormat' => 'number', 'XAxisUnit' => '', 'YAxisFormat' => 'number', 'YAxisUnit' => '', 'XLogo' => 0, 'YLogo' => 0, 'Xlabel' => 'x label', 'XAngle' => 0, 'Ylabel' => 'y label', 'Legend' => '', 'Textbox' => '', 'TextboxFontSize' => 8, 'TextboxFontName' => 'tahoma.ttf', 'ImgR' => 132, 'ImgG' => 173, 'ImgB' => 131, 'Decay' => 80, 'BGR' => 163, 'BGG' => 203, 'BGB' => 167, 'Decay2' => 80, 'Filled' => '', 'DataR' => 191, 'DataG' => 120, 'DataB' => 71, 'LBR' => 226, 'LBG' => 228, 'LBB' => 230, 'LR' => 0, 'LG' => 0, 'LB' => 0); // Get the custom settings if (is_array($config)) { foreach ($config as $key => $val) { $settings[$key] = $val; } } $DataSet = new pData(); $DataSet->AddPoint($y, "Serie1"); $DataSet->AddPoint($x, "Serie2"); $DataSet->AddAllSeries(); $DataSet->RemoveSerie("Serie2"); $DataSet->SetAbsciseLabelSerie("Serie2"); $DataSet->SetSerieName($settings['Legend'], "Serie1"); $DataSet->SetYAxisName($settings['Ylabel']); $DataSet->SetXAxisName($settings['Xlabel']); $DataSet->SetXAxisFormat($settings['XAxisFormat']); if (strlen($settings['XAxisUnit'])) { $DataSet->SetXAxisUnit($settings['XAxisUnit']); } if (strlen($settings['YAxisUnit'])) { $DataSet->SetYAxisUnit($settings['YAxisUnit']); } $DataSet->SetYAxisFormat($settings['YAxisFormat']); // Initialise the graph $Test = new pChart($w, $h); $Test->drawGraphAreaGradient($settings['ImgR'], $settings['ImgG'], $settings['ImgB'], $settings['Decay'], TARGET_BACKGROUND); $FontSize = $settings['FontSize']; $FontName = $this->_ext_path . "/fonts/" . $settings['FontName']; $Test->setFontProperties($FontName, $FontSize); //Calc Textbox Height if (strlen($settings['Textbox'])) { $TextboxFontSize = $settings['TextboxFontSize']; $TextboxFontName = $this->_ext_path . "/fonts/" . $settings['TextboxFontName']; $Position = imageftbbox($TextboxFontSize, 0, $TextboxFontName, $settings['Textbox']); $TextboxHeight = $Position[1] - $Position[7] + 8; } else { $TextboxHeight = 0; } // Maximize The graph area //on Y axis if ($settings['XAxisFormat'] == 'time') { $xdata = "99:99:99"; $Position = imageftbbox($FontSize, 0, $FontName, $xdata); $WXmax = $Position[2] - $Position[0]; $TextHeightX = $Position[1] - $Position[7]; } elseif ($settings['XAxisFormat'] == 'date') { $xdata = "99/99/9999"; $Position = imageftbbox($FontSize, 0, $FontName, $xdata); $WXmax = $Position[2] - $Position[0]; $TextHeightX = $Position[1] - $Position[7]; } else { $WXmax = 0; foreach ($x as $xdata) { $xdata .= $settings['XAxisUnit']; $Position = imageftbbox($FontSize, 0, $FontName, $xdata); $TextWidth = $Position[2] - $Position[0]; $TextHeightX = $Position[1] - $Position[7]; $WXmax = $TextWidth > $WXmax ? $TextWidth : $WXmax; } } if ($settings['XAngle'] > 0) { $sin = abs(sin(deg2rad($settings['XAngle']))); $cos = abs(cos(deg2rad($settings['XAngle']))); $HXmax = $WXmax * $sin + $TextHeightX * $cos; } else { $HXmax = $TextHeightX; } //on Y axis... if ($settings['YAxisFormat'] == 'time') { $ydata = "99:99:99"; $Position = imageftbbox($FontSize, 0, $FontName, $ydata); $WYmax = $Position[2] - $Position[0]; $TextHeightY = $Position[1] - $Position[7]; } elseif ($settings['YAxisFormat'] == 'date') { $ydata = "99/99/9999"; $Position = imageftbbox($FontSize, 0, $FontName, $ydata); $WYmax = $Position[2] - $Position[0]; $TextHeightY = $Position[1] - $Position[7]; } else { $WYmax = 0; foreach ($y as $ydata) { $ydata .= $settings['YAxisUnit']; //echo $ydata."<br>"; $Position = imageftbbox($FontSize, 0, $FontName, $ydata); $TextWidth = $Position[2] - $Position[0]; $TextHeightY = $Position[1] - $Position[7]; $WYmax = $TextWidth > $WYmax ? $TextWidth : $WYmax; } } $Test->setGraphArea($WYmax + $TextHeightY + 35, 20, $w - 25, $h - $HXmax - $TextHeightX - $TextboxHeight - 20); //$Test->setGraphArea(60,20,$w-25,($settings['XAngle']==0)?$h-70:$h-100); $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_ADDALL, 213, 217, 221, TRUE, $settings['XAngle'], 0, TRUE); $Test->drawGraphAreaGradient($settings['BGR'], $settings['BGG'], $settings['BGB'], $settings['Decay2']); $Test->drawGrid(4, TRUE, 230, 230, 230, 20); // This will put the picture "logo.png" with transparency if (strlen($settings['Logo'])) { $XLogo = $WYmax + $TextHeightY + 35 + $settings['XLogo']; $YLogo = 20 + $settings['XLogo']; $logo = $settings['Logo']; //Fing extension of logo : png,gif or jpg if ($this->_findexts($logo) == "png") { echo "png!"; $Test->drawFromPNG($logo, $XLogo, $YLogo, $settings['LogoTransparency']); } elseif ($this->_findexts($logo) == "gif") { echo "gif!"; $Test->drawFromGIF($logo, $XLogo, $YLogo, $settings['LogoTransparency']); } elseif ($this->_findexts($logo) == "jpg") { echo "jpg"; $Test->drawFromJPG($logo, $XLogo, $YLogo, $settings['LogoTransparency']); } } $Test->setColorPalette(0, $settings['DataR'], $settings['DataG'], $settings['DataB']); if ($type == "bar") { // Draw the bar chart $Test->drawStackedBarGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 70); } elseif ($type == "line") { $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription()); $Test->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 3, 0, -1, -1, -1, TRUE); } elseif ($type == "cubic") { $Test->setShadowProperties(3, 3, 0, 0, 0, 30, 4); $Test->drawCubicCurve($DataSet->GetData(), $DataSet->GetDataDescription()); $Test->clearShadow(); if ($settings['Filled'] == 'yes') { $Test->drawFilledCubicCurve($DataSet->GetData(), $DataSet->GetDataDescription(), 0.1, 30); } $Test->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 3, 0, -1, -1, -1, TRUE); } // Draw the textbox if (strlen($settings['Textbox'])) { $Test->setFontProperties($TextboxFontName, $TextboxFontSize); $Test->drawTextBox(0, $h - $TextboxHeight, $w, $h, $settings['Textbox'], 0, 255, 255, 255, ALIGN_CENTER, TRUE, 0, 0, 0, 30); } // Draw the legend if (strlen($settings['Legend'])) { $LegendFontSize = $settings['LegendFontSize']; $LegendFontName = $this->_ext_path . "/fonts/" . $settings['LegendFontName']; $Position = imageftbbox($LegendFontSize, 0, $LegendFontName, $settings['Legend']); $LegendW = $Position[2] - $Position[0] + 40; $Test->setFontProperties($LegendFontName, $LegendFontSize); $Test->drawLegend($w - $LegendW, 10, $DataSet->GetDataDescription(), $settings['LBR'], $settings['LBG'], $settings['LBB'], 52, 58, 82, $settings['LR'], $settings['LG'], $settings['LB'], TRUE); } // Render the picture $Test->addBorder(2); if (strlen($imgname)) { //custom image name $imgname = $this->_img_path . "/" . $imgname; } else { $this->obj->load->helper('string'); $imgname = $this->_img_path . "/{$type}-" . random_string('alnum', 16) . ".png"; } $Test->Render($imgname); return array("name" => '/' . $imgname, "w" => $w + 4, "h" => $h + 4); }