/** * Add links to clickable SVG elements in a chart with SVG driver. * * @param ezcGraphChart $chart * @return void */ public static function linkSvgElements(ezcGraphChart $chart) { if (!$chart->driver instanceof ezcGraphSvgDriver) { throw new ezcGraphToolsIncompatibleDriverException($chart->driver, 'ezcGraphSvgDriver'); } $fileName = $chart->getRenderedFile(); if (!$fileName) { throw new ezcGraphToolsNotRenderedException($chart); } $dom = new DOMDocument(); $dom->load($fileName); $xpath = new DomXPath($dom); $elements = $chart->renderer->getElementReferences(); // Link chart elements foreach ($elements['data'] as $dataset => $datapoints) { foreach ($datapoints as $datapoint => $ids) { $url = $chart->data[$dataset]->url[$datapoint]; if (empty($url)) { continue; } foreach ($ids as $id) { $element = $xpath->query('//*[@id = \'' . $id . '\']')->item(0); $element->setAttribute('style', $element->getAttribute('style') . ' cursor: ' . $chart->driver->options->linkCursor . ';'); $element->setAttribute('onclick', "top.location = '{$url}'"); } } } // Link legend elements if (isset($elements['legend'])) { foreach ($elements['legend'] as $objectName => $ids) { $url = $elements['legend_url'][$objectName]; if (empty($url)) { continue; } foreach ($ids as $id) { $element = $xpath->query('//*[@id = \'' . $id . '\']')->item(0); $element->setAttribute('style', $element->getAttribute('style') . ' cursor: ' . $chart->driver->options->linkCursor . ';'); $element->setAttribute('onclick', "top.location = '{$url}'"); } } } $dom->save($fileName); }