SVG:如何从文本锚设置为中间的多行文本中删除tspan的偏移量?

时间:2017-04-01 17:44:03

标签: svg text tspan

我有一些多行文字,我使用<text><tspan>来处理这个问题。我希望每一行都居中,因此我在主<text>标记text-anchor="middle"中使用。但是,即使使用dx=0,整个块仍会按总长度文本移动。

如何进行多行<tspan>居中的SVG文字?

e.g。

<text text-anchor="middle" font-size="12px">
    This would normally be centered
    <tspan>
        this should be too.
    </tspan>
</text>

1 个答案:

答案 0 :(得分:1)

您可以为tspan指定与文本相同的x,例如

#####------------------------------- 

    # G for GENERATOR No. 
# For each generator we have the following data:
## Maximum Capacity: the most power it can be producing 
## Technical Minimum Capacity: the smallest amount (other than being off)
## Cost per Megawatt: The cost of generating power ($/MW) 
## Ramp Rate: The speed a plant can change to a higher or lower output (MW/min) 

G1 <- c(200,  100,  50,   2)
G2 <- c(150,   10,  80,  10)
G3 <- c(200,  100,  55,   2)
G4 <- c(150,   10,  85,  10)

Gdat.1 <- rbind(G1,G2,G3,G4) 
colnames(Gdat.1) = c("MWMax","MWMin","Cost","RampRate")
Gdat.1
n <- nrow(Gdat.1) # number of generators
#####-------------------------------

# System Requirements: Demand
## Supply (MW) must equal demand. 
Demand <- 415

# System Requirements: Reserves
## Total Reserves of the system must be met.
### R1: Primary Reserves
##### 0.5 minute response time, bi-direcitonal (Ramp UP/DOWN) 
### R2: Secondary Reserves
##### 5 minute response time, bi-directional (Ramp UP/DOWN)
### R3: Tertiary Reserves
##### 15 minute response time, uni-directional (Ramp UP only)

# R for Reserve Type. 
# For each Reserve Type we have the following data:
# Total: MW Needed 
# minutes: within how much time the MW is needed by
# bid: amount the operator will pay for MW reserves $/MW)
R1   <- c(2,  0.5,  60) # Primary 
R2   <- c(8,    5,  40) # Secondary
R3   <- c(20,  15,   0) # Tertiary
Reserves <- rbind(R1,R2,R3)
colnames(Reserves) = c("Total","Minutes","Bid")
Reserves

#####-------------------------------    

## Ramp Rate constraint of generators
### For R1 (Primary Reserves) the system needs 2 MW that can be supplied 30 seconds, 
### a Generator with a ramprate of 2 MW/min will only be able to supply 
### 1 MW for primary reserves, while a Generator with a ramprate of 10 MW/min
### will be able to supply 5 MW.

# How much each Generator can supply in the time time
R1max <- Gdat.1[,"RampRate"] * Reserves["R1","Minutes"] 
R2max <- Gdat.1[,"RampRate"] * Reserves["R2","Minutes"]
R3max <- Gdat.1[,"RampRate"] * Reserves["R3","Minutes"]
R1min <- -R1max  # recall, bi-directional
R2min <- -R2max  # bi-directional
R3min <- 0/R3max # uni-direction up

# we no longer need RampRate since we used it to calculate
Gdat <- cbind(Gdat.1[,-4], cbind(R1max,R2max,R3max,R1min,R2min,R3min))

#####-------------------------------

# Now we initialize each generator's commitments that can change during optimization
MW.Demand = rep(0,n) # general MW to satisfy demand
MW.R1 = rep(0,n) # MW to satisfy Primary Reserves
MW.R2 = rep(0,n) # MW to satisfy Secondary Reserves
MW.R3 = rep(0,n) # MW to satisfy Tertiary Reserves

Commit.orig <- cbind(MW.Demand,MW.R1,MW.R2,MW.R3)
rownames(Commit.orig) <- paste0("G",seq(1,n))
Commit <- Commit.orig
# Some initial guess (may be exactly the right answer...)
Commit <- matrix(c(200,0,0,0,
                   17.5,1,6.5,20,
                   197.5,1,1.5,0,
                   0,0,0,0), 4, 4, byrow = T, dimnames(Commit.orig))

#####-------------------------------

# Objective Function, cost per MW of each generator times their total MW output
# minimize the total cost, not sure which way to list it, or if this way even works
sum(Commit * Gdat[,"Cost"])
sum(Gdat[,"Cost"] %*% Commit)
sum(rowSums(Ctest * Gdat[,"Cost"]))

#####-------------------------------

# Constraints
sum(Commit[,"MW.Demand"]) == Demand & # All generators together must sum to meet system demand requirements
  sum(Commit[,"MW.R1"]) == Reserves["R1","Total"] & # Total Primary Reserves are met
  sum(Commit[,"MW.R2"]) == Reserves["R2","Total"] & # Total Secondary
  sum(Commit[,"MW.R3"]) == Reserves["R3","Total"] & # Total Tertiary
  (rowSums(Commit) <= Gdat[,"MWMax"] | rowSums(Commit) == 0) & # Generators must be less than or equal to its max, or off
  (rowSums(Commit) >= Gdat[,"MWMin"] | rowSums(Commit) == 0) & # Genreators must be more than or equal to its min, or off
  Commit[,"MW.R1"] <= Gdat[,"R1max"] & Commit[,"MW.R1"] >= Gdat[,"R1min"] & # Genrators cannot exceed ramprate limitations
  Commit[,"MW.R2"] <= Gdat[,"R2max"] & Commit[,"MW.R2"] >= Gdat[,"R2min"] & # - for the bi-directional 
  Commit[,"MW.R3"] <= Gdat[,"R3max"] & Commit[,"MW.R3"] >= Gdat[,"R3min"]  # - or unidirectional reserves

或使用转换并为tspan设置x =“0”...

<svg>
<text x="100" y="30" text-anchor="middle" font-size="12px">
    This would normally be centered
    <tspan x="100" dy="30">
        this should be too.
    </tspan>
</text>
</svg>