kinimesi / cytoscape-svg

A Cytoscape.js extension to export the current graph view as an SVG.
https://kinimesi.github.io/cytoscape-svg
GNU General Public License v3.0
34 stars 5 forks source link

'edge-text-rotation': 'autorotate' breaks colors #7

Closed KonradHoeffner closed 2 years ago

KonradHoeffner commented 3 years ago

As soon as I display any labels using 'edge-text-rotation': 'autorotate', the SVG loses all colors and just becomes black and white. See the attached image of a correctly displayed and an incorrectly displayed image. As soon as I remove the edge text rotation, it works again.

svg-autorotate-bug

Selector

  {
      'selector': 'edge',
      'css':
      {
        'z-compound-depth': 'bottom',
        'width': 2.0,
        'edge-text-rotation': 'autorotate',
        'label': function(edge)
        {
          return "bla";
        },
}
rob-gordon commented 3 years ago

I forked this repo in hopes of fixing this but realized this uses https://github.com/gliffy/canvas2svg under the hood and I wasn't really ready to dig into that.

I wrote a temporary fix for my case which uses DOMParser to edit the generated svg and patch the missing fill colors. Maybe it will work for your use case? You may have to adjust the fill color accordingly.

const svgStr = cy.current.svg({ full: true, scale: 2 });
const domparser = new DOMParser();
let svgEl = domparser.parseFromString(svgStr, "image/svg+xml");
let squares = [...svgEl.children[0].querySelectorAll("path")].filter(
  (x) =>
    !x.getAttribute("fill") &&
    x.getAttribute("paint-order") === "fill stroke markers"
);
squares = [...squares, ...svgEl.children[0].querySelectorAll("rect")];
squares.forEach((el) => el.setAttribute("fill", "#ffffff")); // <- set appropriate fill color here
const correctedSvgStr = svgEl.documentElement.outerHTML;
kinimesi commented 3 years ago

It looks like once the rotation of text is set, all parameters of existing nodes are reset. This is the commit that causes this issue: https://github.com/gliffy/canvas2svg/commit/c7df7fa81e9d8b392d0fc2a894f72af7ba54e306 I revered it in my fork: https://github.com/kinimesi/canvas2svg/commit/68b8ef886ca066d07294d9ee3157f6a3a9e44599 It looks like it works, but I'm not exactly sure if it breaks something else. I pushed the fixed version to unstable branch. Could you please test it and let me know if it works?

karasma3 commented 3 years ago

Hello @kinimesi . I tried your fork and it looks like it is #working! Other entities of the graph are not affected by this forked repo.

It looks like not only text-rotation: autorotate, but whole text-rotation attribute is not working. Node and links to be shown: svgBefore Master branch: svgAfter and the result with the fork: svgFork

Can you propose the pull request to the canvas2svg? Or perhaps make cytoscape-svg dependant on your forked fix?

Best regards, Maroš

kinimesi commented 2 years ago

Hi @karasma3, I'm confused by this: It looks like not only text-rotation: autorotate, but whole text-rotation attribute is not working. Does the fix solve all of these issues?

KonradHoeffner commented 2 years ago

Is there an update on this?

kinimesi commented 2 years ago

Hi, could you please try v0.4.0 and let me if it solves your problem?

KonradHoeffner commented 2 years ago

Yes, it works, thanks a lot!