My boss wants several Mathematica graphics as vector graphics in EMF format. All my plots are exported neatly, except for a BarChart which gets rasterized for no apparent reason:
Export["test.emf", BarChart[{1, 2, 3}]];
The resulting test.emf is almost 1 MB and clearly rasterized.
I found an older threat on the subject, but was wondering whether there is a new solution to the problem.
Thank you!
Answer
UPDATE
It seems that I found a straightforward way to disable rasterization while preserving the original appearance: the key is to add the ChartBaseStyle -> EdgeForm[Directive[GrayLevel[0.356], Opacity[1]]]
option. The offending dot bug (demonstrated below in the "Original answer" section) we steel have to fix separately via post-processing /. {Opacity[0], __} -> {}
:
plot = BarChart[{1, 2, 3},
ChartBaseStyle ->
EdgeForm[Directive[GrayLevel[0.356], Opacity[1]]]] /. {Opacity[0], __} -> {}
Export["test.emf", plot]
The above produces a 15 kb EMF file without rasterization. I have checked this workaround in Mathematica 7.0.1, 8.0.4 and 10.0.1 and it seems to work reliably. Note that the dot bug was introduced in version 8.
Original answer
Removing the transparency solves the problem:
Export["test.emf",
BarChart[{1, 2, 3},
PerformanceGoal -> "Speed"] /. {{Opacity[0], __} :> (## &[]), Opacity[_] :> Opacity[1]}]
The above produces a 15 kb file without rasterization.
The first replacement {Opacity[0], __} :> (## &[])
is necessary for removing the redundant black dot at the axis origin:
BarChart[{1, 2, 3}, PerformanceGoal -> "Speed"] /. Opacity[_] -> Opacity[1]
In this concrete case the original appearance can be reproduced by replacing non-zero Opacity
with GrayLevel
:
Export["test.emf",
BarChart[{1, 2, 3},
PerformanceGoal -> "Speed"] /. {{Opacity[0], __} :> (## &[]),
Opacity[o_] :> GrayLevel[1 - o]}]
More general way for removing transparency provides Blend
:
Opacity[a_, color_] :> Blend[{White, color}, a]
Unfortunately the form Opacity[a, color]
is rarely used and inspection of the low-level structure of graphics is usually necessary for finding a way to reproduce the original appearance. For this task I recommend my shortInputForm
function.
Comments
Post a Comment